How to make a user helper logged in Asp.NET MVC 4?

3

I need to implement a Helper for logged in user, ie a static class that returns information about the logged in user of context . The idea is to use information in both Controllers and Views .

    
asked by anonymous 22.05.2014 / 00:38

2 answers

4

@LuizNegrini You can try to use a singleton in the ProfessionalUser class. By adding a static property of the same class type, when calling you can fetch the current user through another external method and using the encrypted user's id as a parameter in a cookie or session.

namespace Aplicacao.Model
{
    public class ProfessionalUser
    {
        private static ProfessionalUser _user;
        private static string _keyUser = "idUserOuQualquerChaveQueVoceEscolher";

        public int IdProfessionalUser { get; set; }

        public string Email { get; set; }

        public string ReEmail { get; set; }

        public string Password { get; set; }

        public string RePassword { get; set; }

        public string Name { get; set; }

        public int IdProfessionalRegister { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public int Phone { get; set; }

        public static ProfessionalUser UsuarioAtual
        {
            get
            {
                if(_user == null)
                {
                    int idUser = 0;
                    HttpCookie cookie = HttpContext.current.Request.Cookies[_keyUser];
                    string v = cookie != null ? cookie.Value : String.Empty;
                    int.TryParse(v, out idUser);
                    _user = (new QualquerGerenciadorDeUsuario()).FuncaoQuePegaOUsuarioPeloID(idUser);
                }
                return _user;
            }
            set
            {
                int idUser = 0;
                if(value != null && value.ID > 0)
                {
                    idUser = value.ID;
                    _user = value;
                }else
                    _user = null;

                HttpCookie cookie = new HttpCookie(_keyUser, idUser.ToString());
                HttpContext cxt = HttpContext.current;
                cookie.Expires = DateTime.Today.AddHours(3);// o cookie vale por 3 horas
                cxt.Response.Cookies.Add(cookie);
            }
        }
    }
}

In View , you can call the property at any time in two ways:

@Aplicacao.Model.ProfessionalUser.UsuarioAtual

And from there you can grab any and all property of the current user:

@ Application.Model.ProfessionalUser.Usuario Actual.IdProfessionalUser

The other form is including namespace in View :

@using Aplicacao.Model
@ProfessionalUser.UsuarioAtual

... and the properties is the same:

@ProfessionalUser.UsuarioAtual.IdProfessional

I hope you have helped.

Att, Uilque Messias

    
22.05.2014 / 23:46
2

Here is a% of what I've done to get information from the logged-in user.

This project uses Helper , that is, it is an old approach, but it serves as an example for improvements in the ASP.NET Identity case:

public static class LoggedUserHelper
{
    private static MyProjectContext context = new MyProjectContext();

    public static UserProfile CurrentUserInfo(IPrincipal User) {
        int currentUserId = WebSecurity.GetUserId(User.Identity.Name);
        return context.UserProfile.AsNoTracking().SingleOrDefault(x => x.UserId == currentUserId);
    }

    public static int UserId(IPrincipal User) {
        return WebSecurity.GetUserId(User.Identity.Name);
    }

    public static int UserId(String UserName) {
        return WebSecurity.GetUserId(UserName);
    }

    ...
}

Obviously my Model Membership has several additional columns, but I will only put the minimum necessary to work:

public class UserProfile {
    [Key]
    public int UserId { get; set; }

    ...
}
    
22.05.2014 / 00:52