User.Identity.Name is always null when using AspNetIdentity with IdentityServer3

I have been trying to install a new IdentityServer3 with AspNetIdentity for several days. I can log in using my existing Identity database, and that is fine, but I can never force User.Identity.Name to contain data. I tried several attempts to add custom requests and areas and add areas for clients.

Finally, I downloaded the IdentityServer3 test repository and tested it using the webforms client project, since it already used User.Identity.Name in it About page.

Using the WebForms + Client Example Sample Server Example AspNetIdentity = User.Identity.Name is always null .Identity.Name.

Now, on the client side, I wrote a workaround to get the value of the "preferred_username" request and give it a "name".

var id = new claimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType); id.AddClaims(userInfoResponse.GetClaimsIdentity().Claims); //set the User.Identity.Name value var name = id.Claims.Where(x => x.Type == "name").Select(x => x.Value).FirstOrDefault() ?? id.Claims.Where(x => x.Type == "preferred_username").Select(x => x.Value).FirstOrDefault(); id.AddClaim(new Claim("name", name)); 

My questions:

  • Why doesn't the AspNetIdentity package populate this by default?
  • And what do I need to change on the server side so that I do not need to change the client?
+6
source share
2 answers
 public static IEnumerable<ApiResource> GetApis() { return new ApiResource[] { new ApiResource("AenSAPI", "A&S Admin software API") { UserClaims = { JwtClaimTypes.Name, JwtClaimTypes.Email } } }; } 

In Identityserver4, you can add UserClaims to your resource. Fixed it for me.

0
source

On IdentityServer4, you can implement IProfileService on the server and add a claim to GetProfileDataAsync.

 public class AspNetIdentityProfileService : IProfileService { protected UserManager<ApplicationUser> _userManager; public AspNetIdentityProfileService(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public Task GetProfileDataAsync(ProfileDataRequestContext context) { //Processing var user = _userManager.GetUserAsync(context.Subject).Result; var claims = new List<Claim> { new Claim(ClaimTypes.Name, user.UserName), }; context.IssuedClaims.AddRange(claims); //Return return Task.FromResult(0); } public Task IsActiveAsync(IsActiveContext context) { //Processing var user = _userManager.GetUserAsync(context.Subject).Result; context.IsActive = (user != null) && ((!user.LockoutEnd.HasValue) || (user.LockoutEnd.Value <= DateTime.Now)); //Return return Task.FromResult(0); } } 

Then add "AddProfileService ()" to your ConfigureServices method.

 services.AddIdentityServer(...) ... .AddProfileService<AspNetIdentityProfileService>(); 
-1
source

Source: https://habr.com/ru/post/1264692/


All Articles