开发者

FormsAuthentication friendly name

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开发者_Go百科.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 the PostAuthenticated event where you instantiate your principal object and set CurrentUser to that instance. Now anytime you access CurrentUser you will get your instance of the IPrincipal that is decorated with all of the extra data you need.


I think the best way to do this is to use a common view model that has this property. Have all of your other view models derive from this model. Use a base controller and override the OnActionExecuted method, setting the common view model properties when the result being returned is a ViewResult. Your views would be strongly typed either to the common view model or a subclass from it, allowing you to reference the properties directly.

 public class CommonViewModel
 {
     public string UserDisplayName { get; set; }
     public string Username { get; set; }
 }

 public class FooViewModel : CommonViewModel
 {
     // view-specific properties
 }

 public class BaseController : Controller
 {
     public override void OnActionExecuted( ActionExecutedContext context )
     {
         if (context.Result is ViewResult)
         {
              UpdateCommonModel( ((ViewResult)context.Result).ViewData.Model as CommonViewModel );
         }
     }

     private void UpdateCommonModel( CommonViewModel model )
     {
          User user = authService.ValidateUser(model.Email, model.Password);
          modelUserDisplayName =  string.Format("{0} {1}", user.FirstName, user.LastName);
          model.Username = user.Name;
     }
 }

View

 @if (Request.IsAuthenticated)
 {
      @Html.ActionLink(model.UserDisplayName, "Details", "Users", new { id = Model.Username });
 }


If you're using MVC 3 you can use a global filter instead if you really did not want to add a base controller.


Here is a good article on this topic, and it has been written in regards of MVC 4:

http://www.codeproject.com/Tips/574576/How-to-implement-a-custom-IPrincipal-in-ASP-NET-MV

There are two little mistakes in the code, but I have pointed them out in a comment at the bottom of the article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜