MVC 5全局用户帐户对象

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有更多属性可以设置.

3dd*_*3dd 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);
Run Code Online (Sandbox Code Playgroud)