I'm developing an ASP.Net MVC5 web app that needs to connect to an existing system. The site authentication uses the standard out of the box classes you get when creating new ASP web app but stripped down so that an Admin user can only register a new account. The database with the user accounts being held in App_data.
Confirming login needs to be two part process. User account must not only exist in the local DB holding the web accounts, they must also exists in out main system. This has been done by changing the AccountController login action so that when confirmed by the SignInManager, we then connect to the other database and validate the user account.
Once the user account has been validated in the other database I would like to add the ID as a Claim to current User. How do I do this? User.Identity is currently GenericIdentity.
public async Task<ActionResult> Login( LoginViewModel model, string returnUrl)
{
...
...
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); if (result == SignInStatus.Success) { try { var nssUser = myOtherDB.UserRepository.FetchByEmail(model.Email); if (nssUser == null || !nssUser.AllowWebAccess) { authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); result = SignInStatus.Failure; } else // user is confirmed in other db {
// ********************************************************************* // I would like to add the nssUser.UserID as a claim to the User.Identity at this point
// ********************************************************************** } } catch { authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); result = SignInStatus.Failure; } } switch (result) { case SignInStatus.Success: return redirectToLocal(returnUrl); case SignInStatus.LockedOut: return View("Lockout"); case SignInStatus.RequiresVerification: // this is for code notification via phone. Email varification is captured above case SignInStatus.Failure: default: ModelState.AddModelError("", "Invalid login attempt."); return View(model); } ....
Thanks in advance
Andrew