How to make a logged in user helper Asp.NET MVC 4?
I need to implement a logged-in user Helper, i.e. a static class that returns information about the logged-in user of context
. The idea is to use the information in both Controllers
and Views
.
2 answers
@ LuizNegrini you can try using a singleton in the ProfessionalUser class. By adding a static property of the same type as the class, when called you can fetch the current user through another external method and using the encrypted user id in a cookie or session as a parameter.
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);
}
}
}
}
Already in View
, you can call the property at any time in two ways:
@Aplicacao.Model.ProfessionalUser.UsuarioAtual
And from there you can take any and all user property current:
@Aplicacao.Model.ProfessionalUser.Current user.IdProfessionalUser
The other way is by including namespace
in View
:
@using Aplicacao.Model
@ProfessionalUser.UsuarioAtual
... and the properties is the same:
@ProfessionalUser.UsuarioAtual.IdProfessional
I hope it helped.
Att, Uilque Messiah
Follows a Helper
I did to get logged in user information.
This project uses Membership
, that is, it is an old approach, but it serves as an example for improvements in the case of ASP.NET Identity:
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 UserProfile
has several additional columns, but I will put only the minimum necessary to work:
public class UserProfile {
[Key]
public int UserId { get; set; }
...
}