Is there a way to set a friendly name on FormsAuthentication so I can have access to both the ID and friendly name in the Context.User.Identity
I’d Like to display the First/Last name with a url pointing to the profile page of the user by the userid.
This is what I currently have:
View
@var user = Context.User.Identity;
@if (Request.IsAuthenticated)
{
@Html.ActionLink(user.Name, "Details", "Users", new { id = user.Name });
}
As you can see, it will show only the UserId.
Controller
User user = authService.ValidateUser(model.Email, model.Password);
string alias = string.Format("{0} {1}", user.FirstName, user.LastName);
//I'd like some way to set both the user.UserId and alias in the cookie and access it in the view
FormsAuthentication.SetAuthCookie(user.UserId, createPersistentCookie);
Yes, just create your own implementation of
IPrincipal. Then hook up an HttpModule to thePostAuthenticatedevent where you instantiate your principal object and set CurrentUser to that instance. Now anytime you access CurrentUser you will get your instance of theIPrincipalthat is decorated with all of the extra data you need.