我有一个具有以下布局的应用程序。在我的共享 View 文件夹中,_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
返回该用户名。但是我需要显示用户的全名。当我们进行身份验证时,我可以访问全名。但不确定如何使用它。
如何将 AccountController
中的 FullName
值传递给 _CurrentUser.cshtml
部分 View ?有点像 @User.Identity
这样的全局容器,可以设置更多属性。
最佳答案
你可以这样使用
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);
关于c# - MVC 5 全局用户帐户对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529401/