Sha*_*Wyk 5 c# asp.net asp.net-mvc razor asp.net-mvc-4
我有一个具有以下布局的应用程序.在共享视图文件夹中,我有_Layout.cshtml
,_SideNav.cshtml
和_CurrentUser.cshtml
.
在_Layout.cshtml
我有:
@{ Html.RenderPartialIf("_SideNav", Request.IsAuthenticated); }
Run Code Online (Sandbox Code Playgroud)
在_SideNav.cshtml
我有:
@{ Html.RenderPartial("_CurrentUser"); }
Run Code Online (Sandbox Code Playgroud)
在_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>
Run Code Online (Sandbox Code Playgroud)
我们FormsAuthentication
用来验证用户.我们没有使用Identity
随附的标准身份验证,ASP.Net MVC 5
因为我们使用的是LDAP
服务器.
FormsAuthentication.SetAuthCookie(username, isPersistent);
.....
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(username, "Forms"), roles);
Run Code Online (Sandbox Code Playgroud)
我们username
在cookie中使用,以便我们可以轻松地从LDAP
服务器获取信息.
问题:@User.Identity.Name
返回该用户名.但我需要显示用户的全名.我们在进行身份验证时可以访问全名.但不知道如何使用它.
如何将FullName
值传递AccountController
给_CurrentUser.cshtml
局部视图?有点像全球容器,@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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
798 次 |
最近记录: |