In my code (ASP.NET Identity 2.1) I install the following statements:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType); ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = CreateProperties( user.UserName, oAuthIdentity, user.FirstName, user.LastName, user.Organization); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); }
Here is the CreateProperites method in which role requests are added:
public static AuthenticationProperties CreateProperties( string userName, ClaimsIdentity oAuthIdentity, string firstName, string lastName, int organization) { IDictionary<string, string> data = new Dictionary<string, string> { { "userName", userName}, { "firstName", firstName}, { "lastName", lastName}, { "organization", organization.ToString()}, { "roles",string.Join(":",oAuthIdentity.Claims.Where(c=> c.Type == ClaimTypes.Role).Select(c => c.Value).ToArray())} }; return new AuthenticationProperties(data); }
On my client, I issue a request for a token and get it like this:
this.$http({ method: 'POST', url: '/Token', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, data: 'grant_type=password&username=' + encodeURIComponent(userName) + '&password=' + encodeURIComponent(password), }) .success((data: any, status, headers, cfg) => { self.data.roles = data.roles;
I see that self.data.roles is correctly filled with roles. Now back to the server, and I want to check the contents of the role requirements. Can someone help by telling me how to do this? I know that I can do the following in a method:
[HttpGet] [Route("Retrieve")] public async Task<IHttpActionResult> Retrieve() { var x = User;
x gets the value
System.Security.Claims.ClaimsPrinciple and System.Security.Claims.ClaimsIdentity
but inside x I cannot find information about the claims themselves.
Note that I tried a sentence posted earlier in SO:
var identity = (ClaimsIdentity)User.Identity; IEnumerable<Claim> claims = identity.Claims;
But I still canβt claim that the information related to the role requirement was found by me. I know that he should be there when he gets to the client.
Please note that I am looking for a specific application. Not any claimed system. But the one I tried to add containing a concatenated list of roles.