I have a application with the following Layout. In the Shared Views Folder I have, _Layout.cshtml
, _SideNav.cshtml
and _CurrentUser.cshtml
.
In the _Layout.cshtml
I have:
@{ Html.RenderPartialIf("_SideNav", Request.IsAuthenticated); }
In the _SideNav.cshtml
I have:
@{ Html.RenderPartial("_CurrentUser"); }
In the _CurrentUser.cshtml
I have:
<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>
We use FormsAuthentication
to authenticate a user. We are not using the standard Identity
authentication which ships with ASP.Net MVC 5
because we are using a LDAP
Server.
FormsAuthentication.SetAuthCookie(username, isPersistent);
.....
HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(username, "Forms"), roles);
We use the username
in the cookie so that we can easily get information from the LDAP
server.
Problem: @User.Identity.Name
returns that username. But I need to display the full name of the user. I have access to the full name when we authenticate. but not sure how to use it.
How can I pass the FullName
value from the AccountController
to the _CurrentUser.cshtml
partial view? Kind of like a Global Container like @User.Identity
with more attributes that can be set.
You can use something like this
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);
You can try something similar to-->
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With