【问题标题】:MVC 5 Global User Account ObjectMVC 5 全局用户帐户对象
【发布时间】: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


【解决方案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);

【讨论】:

  • 您将如何抽象视图中的用户数据?
  • 当用户登录并且您拥有 FullName 时,创建一个包含您想要的所有内容的用户对象。然后将其序列化到cookie中。要在 _CurrentUser.cshtml 中显示详细信息,让该视图使用 User 对象作为 viewModel,然后通过 User 对象渲染屏幕
  • 谢谢我设法让这个工作。在我的.cshtml 文件中,我添加了一个代码块来获取数据。我确信有更好的方法,但它现在可以工作。我之前尝试过类似的解决方案,但由于某种原因它根本不起作用,不知道为什么。
【解决方案2】:

你可以试试类似-->

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

【讨论】:

【解决方案3】:

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

【讨论】:

  • 您在哪里添加声明?
  • 在您的登录控制器中添加声明
  • 这是用于 ASP.NET 身份的。这不适用于 FormsAuthentication。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多