-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem adding custom ClaimsPrincipalFactory when using roles #46593
Comments
Hello @Artiom-Evs Choosing the UserClaimsPrincipalFactory to add custom claims to a specific logged-in user is a good choice. If you want the specific role value to be present in claims, after the user has successfully signed in, try adding the claim to the user 'on the fly'. Please try code below and let me know if it worked.
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
...
if (ModelState.IsValid)
{
...
if (result.Succeeded)
{
if (!await _roleManager.RoleExistsAsync("SomeRole"))
await _roleManager.CreateAsync(new IdentityRole("SomeRole"));
await _userManager.AddToRoleAsync(user, "SomeRole");
var result = await _userManager.AddClaimsAsync(user, new[] {
new Claim(ClaimTypes.Role, "SomeRole"),
});
...
}
...
}
...
}
``` |
Thank you @quicksln! As I understand it, role claims are generated automaticaly by default. I will be very grateful if you give me an answer or tell me where I can read more about this. |
I'm glad to hear that the code worked for your use case scenario :) It's not entirely true that claims are “generated automatically by default”. Usually, “default claims” are assigned to the user during authentication (when using SignInManager) or when the developer decides to do so. When inspecting SignInManager, you will see where IUserClaimsPrincipalFactory is used. When checking UserStore for EntityFrameworkCore, you will notice that the AddToRoleAsync method does not create or assign claims to the user. I think that’s it. |
I found my error.
I Inherited my Thanks for the links to the source code! This helped me find the problem. |
Is there an existing issue for this?
Describe the bug
I am trying to implement role based authentication and custom user claims in my application.
I using ASP.NET Core with React.js project template.
To generate custom user claims, I use the
ApplicationUserClaimsPrincipalFactory
class, which is inherited from theUserClaimsPrincipalFactory
class with theGenerateClaimsAsync
method overridden.The problem is that if
AddClaimsPrincipalFactory
is called afterAddRoles
, only theFullName
field is added to theClaimsPrincipal
, and ifAddClaimsPrincipalFactory
is called beforeAddRoles
, then only therole
field is added to theClaimsPrincipal
.I have created repository that represent this issue.
This issue was also raised in the this issue, but I am not use Blazor and this solution not works for me.
This problem is also in this post on StackOverflow. This solution doesn't work for me either. I have implemented this in the
implement-profile-service
branch.I have debugged in different steps but didn't find the place where the problem occurs, only one required claim existed in each of them.
Expected Behavior
The claims
role
andFullName
are added to theClaimsPrincipal
of the authorized user.Steps To Reproduce
Add support of roles in Program.cs:
Add new custom property to the ApplicationUser class:
Override Register page of Identity UI using aspnet-codegenerator tool:
Add full name editor in the Register.cshtml:
Edit InputModel class in the Register.cshtml.cs:
Edit User object creation in the OnPostAsync method in the Register.cshtml.cs:
Add code for automatically adding all users in the 'SomeRole' role in the Register.cshtml.cs:
Edit Home.js to display JWT data:
Add roles and custom profile claim in the client scopes in the Program.cs:
Add ApplicationUserClaimsPrincipalFactory class:
Add ApplicationUserClaimsPrincipalFactory usage in the services configuration in the Program.cs:
Exceptions (if any)
No response
.NET Version
7.0.100
Anything else?
No response
The text was updated successfully, but these errors were encountered: