MVC 5 Global Account Object

I have an application with the following layout. In the General _Layout.cshtml , I have _Layout.cshtml , _SideNav.cshtml and _CurrentUser.cshtml .

In _Layout.cshtml , I have:

 @{ Html.RenderPartialIf("_SideNav", Request.IsAuthenticated); } 

In _SideNav.cshtml , I have:

 @{ Html.RenderPartial("_CurrentUser"); } 

In _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 the user. We do not use the standard Identity authentication that comes with ASP.Net MVC 5 because we use an 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 receive information from the LDAP server.

Problem: @User.Identity.Name returns this username. But I need to display the full username. I have access to the full name during authentication. but not sure how to use it.

How to pass the FullName value from the AccountController part to a partial view of _CurrentUser.cshtml ? Like a global container like @User.Identity with a lot of attributes that can be set.

+5
source share
3 answers

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); 
+1
source

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; } 
+1
source

Use ClaimsIdentity .

When a user logs into your application, add their name to your identity.

 ClaimsIdentity claims = new ClaimsIdentity(); claims.AddClaim(new Claim("name", "value")); 

Then create an extension method to get your name

  public static string GetName(this IPrincipal principal) { return ((ClaimsIdentity)principal.Identity).Claims.Where(x => x.Type == "name").FirstOrDefault().Value; } 

Now use it as @User.GetName()

Update:

Or in your login controller, use the UserManager to get the user ID. Like this:

 ClaimsIdentity claims = UserManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); claims.Add(new Claim("name" , "value")); AuthenticationManager.SignIn(claims); 
0
source

All Articles