Dynamic User Claims in ASP.NET Identity EF

zaparker

I'm working on an authentication system that uses ASP.NET Identity with Entity Framework, and I want to have a few claims that are computed values instead of being hardcoded into the claims table.

When a user logs in, how can I add dynamic claims to that login session without actually adding them to the claims table?

For example, I may want to store each user's DOB, but I want add IsBirthday as a claim if the login date matches the user's DOB. I don't want to have to store a "IsBirthday" claim for each user since it changes daily for everyone.

In my code, I use this to log in:

var signInResult = await SignInManager.PasswordSignInAsync(username, password, false, false);

After this is called I can reference the ClaimsPrincipal, but the Claims property is an IEnumerable, not a List, so I can't add to it.

EDIT: I should also mention I am using the Microsoft.AspNet.Identity.Owin libraries.

zaparker

OK, everyone, I did a bit of digging into the classes provided in ASP.NET Identity and found the one I needed to override. The SignInManager class has a CreateUserIdentityAsync method that does exactly what I was wanting. The following code added the IsBirthday claim to my identity but didn't store it in the database.

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
  public override async Task<System.Security.Claims.ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
  {
      var identity = await base.CreateUserIdentityAsync(user);
      identity.AddClaim(new System.Security.Claims.Claim("IsBirthday", user.DOB.GetShortDateString() == DateTime.Now.GetShortDateString()));
      return identity;
  }

  // ... EXCLUDING OTHER STUFF LIKE CONSTRUCTOR AND OWIN FACTORY METHODS ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding Claims to an ASP.Net Identity Core user fails with EF insert error

Dynamic claims asp.net core identity no DB persistence

Force another user to refresh their Claims with ASP.NET Identity 2.1.0

ASP.NET Identity: Why User Properties AND Claims?

What is the claims in ASP .NET Identity

IdentitySever4 user claims and ASP.NET User.Identity

ASP.NET Identity Role claims missing

Identity Claims Provider Mapping in ASP.NET

How to get user claims after signin through SignInManager in ASP .NET CORE Identity?

How to get JWT authenticated sign-in user from identity claims in asp.net web api

How many claims can a Role/User have in ASP.Net.Core.Identity?

How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

User's Claims Identity

Not able to get user group claims when using Azure AD as external login alongside Identity Core(ASP.NET Core 2.1)

.net core mvc Mocking Identity and claims and test if user has claim

Authorize with Claims instead of Policies in ASP.Net Identity

Best Practices for Roles vs. Claims in ASP.NET Identity

ASP.NET Identity "Role-based" Claims

ASP .NET CORE 2.2 JWT & Claims identity Authentication for Website

IdentityServer 3 + Asp.net Identity: Scopes, Claims and Clients - Clarifications

How to get max value of claims value in ASP.NET Identity?

Extra column on user-roles table when using custom identity user(ASP.NET 5.2EF 6 and Identity 2)

ASP.Net Core how to get the user role in EF Core and Identity

client specific claims identity server4 using asp.net core identity

ASP.NET Identity Dynamic Role Authorization

how to get claims of another user using ASP.NET Core

asp.net core how to add claims to User

How do I update user claims in Asp.net Core

.NET Core Web API HttpContext.User.Claims and HttpContext.User.Identity are always null in Controllers