JustMaier / MVC5-SPA-Angular

The MVC5 SPA Template Adapted for Angular
Other
36 stars 18 forks source link

Get User.Identity.Name in Other controller #8

Open hosseinhagh66 opened 9 years ago

hosseinhagh66 commented 9 years ago

i need User.Identity.Name in Home Controller but get nothing

JustMaier commented 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?

hosseinhagh66 commented 9 years ago

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