private async Task <UserSummaryViewModel> GetUserData(RobaIdentityUser user) { if (user == null) { return(null); } var roles = await _userManager.GetRolesAsync(user); if (roles.Count == 0) { roles.Add("ApiUser"); } // generate the jwt for the local user... var jwt = await _jwtFactory.GenerateEncodedToken(user.UserName, _jwtFactory.GenerateClaimsIdentity(user.UserName, user.Id.ToString())); var userModel = new UserSummaryViewModel { //JWT could inject all these properties instead of creating a model, //but a model is a little easier to access from client code without //decoding the token. When this user model starts to contain arrays //of complex data, including it all in the JWT value can get complicated. Id = user.Id, Name = user.Name, EmailAddress = user.Email, JwtToken = jwt, Roles = roles.ToArray(), //each role could be a separate claim in the JWT AccountId = 0 //TODO: load this from registration data }; return(userModel); }
public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (!ModelState.IsValid) { return(new ValidationFailedResult(ModelState)); } var user = new RobaIdentityUser { Name = model.Name, UserName = model.EmailAddress, Email = model.EmailAddress }; var result = await _userManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(new ValidationFailedResult(result.Errors.Select(x => x.Description))); } _logger.LogInformation("User created a new account with password."); var identity = await Authenticate(model.EmailAddress, model.Password); var userModel = await GetUserData(identity); return(Ok(userModel)); }
public async Task <IActionResult> PostMicrosoft([FromBody] MicrosoftAuthViewModel model) { var verifier = new MicrosoftAuthVerifier <AuthController>(_microsoftAuthSettings, _configuration["HttpHost"] + (model.BaseHref ?? "/"), _logger); var profile = await verifier.AcquireUser(model.AccessToken); if (!profile.IsSuccessful) { _logger.LogWarning("ExternalLoginCallback() unknown error at external login provider, {profile.Error.Message}", profile.Error.Message); return(new ValidationFailedResult(profile.Error.Message, StatusCodes.Status400BadRequest)); } var info = new UserLoginInfo("Microsoft", profile.Id, "Microsoft"); if (info == null || info.ProviderKey == null || info.LoginProvider == null) { _logger.LogWarning("ExternalLoginCallback() unknown error at external login provider"); return(new ValidationFailedResult("Unknown error at external login provider", StatusCodes.Status400BadRequest)); } if (string.IsNullOrWhiteSpace(profile.Mail)) { return(new ValidationFailedResult("Email address not available from Login provider, cannot proceed.", StatusCodes.Status403Forbidden)); } // ready to create the local user account (if necessary) and jwt var user = await _userManager.FindByEmailAsync(profile.Mail); if (user == null) { var appUser = new RobaIdentityUser { Name = profile.DisplayName, Email = profile.Mail, UserName = profile.Mail, PhoneNumber = profile.MobilePhone }; var identityResult = await _userManager.CreateAsync(appUser, Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 8) + "#"); if (!identityResult.Succeeded) { return(new ValidationFailedResult("Could not create user.", StatusCodes.Status400BadRequest)); } user = await _userManager.FindByEmailAsync(profile.Mail); if (user == null) { return(new ValidationFailedResult("Failed to create local user account.", StatusCodes.Status400BadRequest)); } } var userModel = await GetUserData(user); return(Ok(userModel)); }
public void UpdateUser(RobaIdentityUser user) { context.Update(user); }