【发布时间】:2015-07-21 02:29:18
【问题描述】:
我有一个具有以下布局的应用程序。在我的共享视图文件夹中,_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 这样的全局容器,具有更多可以设置的属性。
【问题讨论】:
-
您是否在身份验证 cookie 中存储自定义数据?
-
不,只是按照上面的方法开箱即用。我尝试了一种存储自定义数据的解决方案,但没有成功,它根本不允许我进行身份验证。如果你有一个解决方案让我尝试,我会试一试。
-
FormsAuthenticationTicket 包含一个 UserData 属性。这是免费使用的。解密票证后,您可以通过自定义对象读写访问它。在票证有效期间,自定义对象可以包含您需要的其他信息。
-
我尝试了在网上找到的解决方案,但由于某种原因根本无法进行身份验证。此外,userdata 是一个字符串类型的对象,因此并不理想,因为稍后需要在其中存储更多附加信息,例如组织名称等
-
如果您的 _CurrentUser.cshtml 在 AccountController 呈现的页面之一中,那么,您应该能够使用控制器来完成繁重的工作(从您的数据库上下文中获取用户信息)。接下来,将其添加到 viewbag 变量中,并在 _CurrentUser 视图中以 @ViewBag.myvar 的形式使用它(如果您愿意,还可以通过视图上的语言代码)
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor