MVC 5全局用户帐户对象

本文关键字:对象 用户 全局 MVC | 更新日期: 2023-09-27 18:03:54

我有一个应用程序与以下布局。在共享视图文件夹中,我有_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来验证用户。我们没有使用ASP.Net MVC 5附带的标准Identity身份验证,因为我们使用的是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,有更多可以设置的属性

MVC 5全局用户帐户对象

你可以这样写

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);

您可以尝试类似于-->

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;       
}

使用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);