MVC 5全局用户帐户对象

时间:2015-07-21 02:29:19

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我有一个具有以下布局的应用程序。在我的共享视图文件夹中,_Layout.cshtml_SideNav.cshtml_CurrentUser.cshtml

_Layout.cshtml我有:

@{ Html.RenderPartialIf("_SideNav", Request.IsAuthenticated); }

_SideNav.cshtml我有:

@{ Html.RenderPartial("_CurrentUser"); }

_CurrentUser.cshtml我有:

<div class="login-info">
    <span>
        <a href="javascript:void(0);" id="show-shortcut" data-action="toggleShortcut">
            <img src="~/content/img/avatars/sunny.png" alt="me" class="online" />
            <span>@User.Identity.Name</span>
            <i class="fa fa-angle-down"></i>
        </a>
    </span>
</div>

我们使用FormsAuthentication来验证用户身份。我们未使用Identity附带的标准ASP.Net MVC 5身份验证,因为我们使用的是LDAP服务器。

FormsAuthentication.SetAuthCookie(username, isPersistent);
.....
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(username, "Forms"), roles);

我们在Cookie中使用username,以便我们可以轻松地从LDAP服务器获取信息。

问题:@User.Identity.Name会返回该用户名。但我需要显示用户的全名。我们在进行身份验证时可以访问全名。但不知道如何使用它。

如何将FullName值从AccountController传递到_CurrentUser.cshtml局部视图?有点像@User.Identity之类的全局容器,可以设置更多属性。

3 个答案:

答案 0 :(得分:1)

您可以尝试类似于 - &gt;

的内容
public static MyAuthenticationTicket  GetIMyUserTicket()
{
    //Get custom user data object from forms auth cookie
    MyAuthenticationTicket  result= null;
    if (HttpContext.Current.User == null)
        return null;
    if (!HttpContext.Current.Request.IsAuthenticated)
        return null;
    FormsIdentity ident = (FormsIdentity)HttpContext.Current.User.Identity;
    if (ident == null)
        return null;
    FormsAuthenticationTicket ticket = ident.Ticket;
    if (ticket == null)
        return null;
    if (!FormsAuthentication.CookiesSupported)
    {               
        //If cookie is not supported for forms authentication, then the 
        //authentication ticket is stored in the Url, which is encrypted.
        //So, decrypt it
        ticket = FormsAuthentication.Decrypt(ident.Ticket.Name);

    }

    string userDataString = ticket.UserData;
    if(!String.IsNullOrEmpty(userDataString))
        result= new MyAuthenticationTicket(userDataString);

    return result;       
}

答案 1 :(得分:1)

你可以使用这样的东西

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                viewModel.Email,
                                                                YOUR_ISSUE_DATE,
                                                                YOUR_EXPIRE_DATE,
                                                                viewModel.RememberMe,
                                                                JsonConvert.SerializeObject(user),
                                                                FormsAuthentication.FormsCookiePath);


string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

Response.Cookies.Add(authcookie);

答案 2 :(得分:0)

使用ClaimsIdentity

当用户登录您的应用时,请将其姓名添加到您的身份中。

ClaimsIdentity claims = new ClaimsIdentity();
claims.AddClaim(new Claim("name", "value"));

然后创建一个扩展方法来获取您的名字

 public static string GetName(this IPrincipal principal)
 {
     return ((ClaimsIdentity)principal.Identity).Claims.Where(x => x.Type == "name").FirstOrDefault().Value;
 }

现在将其用作@User.GetName()

更新

或者在您登录控制器时使用UserManager来获取用户身份。 像这样:

ClaimsIdentity claims = UserManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType);
claims.Add(new Claim("name" , "value"));

AuthenticationManager.SignIn(claims);
相关问题