IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Register new Azure AD user in application

Open shwetabab opened this issue 5 years ago • 4 comments

Hello,

To add new Azure Ad user to application, I used below code snippet,

AppUser appUser = new AppUser() { UserName = user.Email, CreatedDate=DateTime.Now, Email=user.Email }; var result = await _userManager.CreateAsync(appUser); if (!result.Succeeded) { results.AddRange(result.Errors.Select(x => x.Description)); } else { await _userManager.AddClaimAsync(appUser, new System.Security.Claims.Claim("userName", user.Email)); await _userManager.AddClaimAsync(appUser, new System.Security.Claims.Claim("name", userExistsInAD)); await _userManager.AddClaimAsync(appUser, new System.Security.Claims.Claim("email", user.Email)); await _userManager.AddClaimAsync(appUser, new System.Security.Claims.Claim("role", user.Role)); }

This created a new record in AspNetUsers table with PasswordHash null. After creating user, on trying to login, I got below error

Value cannot be null. (Parameter 'value')

I tried to manually set value of PasswordHash, after which I could successfully login into application.

var returnUrl = result.Properties.Items["returnUrl"] ?? "~/"; await HttpContext.SignInAsync(isuser, localSignInProps); var principal = await _signInManager.CreateUserPrincipalAsync(user); additionalLocalClaims.AddRange(principal.Claims); var name = principal.FindFirst(JwtClaimTypes.Name)?.Value ?? user.Id; await _events.RaiseAsync(new UserLoginSuccessEvent(provider, providerUserId, user.Id, name)); await HttpContext.SignInAsync(user.Id, name, provider, localSignInProps, additionalLocalClaims.ToArray());

  // delete temporary cookie used during external authentication
     await HttpContext.SignOutAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);

I tried to implement it the using sample code in quickstart. Still facing this issue. Please help.

Thanks.

shwetabab avatar Mar 01 '21 23:03 shwetabab

Sounds like an issue with ASP.NET Identity -- that's a Microsoft library, so I'd ask in their repo.

brockallen avatar Mar 02 '21 03:03 brockallen

This created a new record in AspNetUsers table with PasswordHash null.

This is the expected behaviour. IdentityServer doesn't/can't know the AD password.

After creating user, on trying to login, I got below error

Are you trying to login as this AD user from ASP.NET Identity username/password control? Credential based login will work only with ASP.NET Identity local accounts. If you want to login via AD, you need to use registered External Authentication button, which will redirect your to AD authentication/login page if you are not logged in already.

rajivverma avatar Mar 04 '21 18:03 rajivverma

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Questions are community supported only and the authors/maintainers may or may not have time to reply. If you or your company would like commercial support, please see here for more information.

stale[bot] avatar Mar 16 '21 04:03 stale[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Apr 04 '21 23:04 github-actions[bot]