Using User.IsInRole returns random result when UserRole changes
Using User.IsInRole returns random result when UserRole changes
I'm using asp.net core 2.1 with default identity settings and every time a role is changed the user should re-login to see the role changes.
If I add the following settings, roles should be updated on each request, the but Authorize
attribute works wellUser.IsInRole()
method returns random results on each request.
Authorize
User.IsInRole()
services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.Zero;
});
What's the problem with User.IsInRole()
? How to fix it?
User.IsInRole()
Edit:
I was incorrect about the Authorize
attribute behavior, that works random too.
Authorize
By random I mean, after I change some roles (only once) when I refresh the page multiple times, sometimes it shows role changes, but sometimes it doesn't. How can I hook that event? If it's by using
services.AddAuthentication().AddCookie
then that doesn't get fired. but the SecurityStampValidator OnRefreshingPrincipal
gets fired every time.– Ashkan Nourzadeh
2 days ago
services.AddAuthentication().AddCookie
OnRefreshingPrincipal
That behavior is strange, out of interest what does
User.Identity.Name
show each time along with role check? If that event doesn't fire that would be odd, maybe set ValidationInterval
to a value other than TimeSpan.Zero
and try again?– Mark G
2 days ago
User.Identity.Name
ValidationInterval
TimeSpan.Zero
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When you say random, do you mean that the changed roles only take sometimes? You could try hooking up OnValidatePrincipal and confirm that it's getting called.
– Mark G
2 days ago