Open hosseinhagh66 opened 9 years ago
Since the Authentication is happening via bearer tokens at the API level you actually can't access User.Identity from the MVC controller level. That's at least my experience... Anyone else know otherwise?
I agree, I really need a user name logged in. For this reason, the username and the respective roles store In clamis, And Anywhere you need clamis retrieve it
ٍEdit this Method public void ConfigureAuth(IAppBuilder app) { ... app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Application"
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
.. } And When User Login public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var authentication = HttpContext.Current.GetOwinContext().Authentication; authentication.SignIn( new AuthenticationProperties(), new ClaimsIdentity(new[] { new Claim( "UserName", userAccount.UserName), new Claim( "Roles", GetRoles(userAccount.Id)) } , "Application")); } And when Need User Claims var authentication = HttpContext.Current.GetOwinContext().Authentication; var ticket = authentication.AuthenticateAsync("Application").Result; var identity = ticket != null ? ticket.Identity : null; //identity.Claims
The method is appropriate? thanks
i need User.Identity.Name in Home Controller but get nothing