public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { try { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); using (AuthRepository _repo = new AuthRepository()) { DubaiCultureUser user = await _repo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } ClaimsIdentity identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Email, user.Email)); identity.AddClaim(new Claim(ClaimTypes.Role, string.Join(",", user.Roles))); identity.AddClaim(new Claim(ClaimTypes.Name, user.FirstName + " " + user.LastName)); context.Validated(identity); } } catch (System.Exception) { throw; } }
public async Task <IdentityResult> RegisterUser(RegisterModel userRegister) { KenticoUserManager <DubaiCultureUser> t = new KenticoUserManager <DubaiCultureUser>(new KenticoUserStore <DubaiCultureUser>(AppConfig.SiteName)); DubaiCultureUser user = new DubaiCultureUser { UserName = userRegister.UserName, FirstName = userRegister.FirstName, LastName = userRegister.LastName, Email = userRegister.UserName, Enabled = true }; IdentityResult result = await _userManager.CreateAsync(user, userRegister.Password); return(result); }
public async Task <IdentityManagerResult <ConfirmUserResultState> > ConfirmUserAsync(int userId, string token, RequestContext requestContext) { IdentityManagerResult <ConfirmUserResultState> accountResult = new IdentityManagerResult <ConfirmUserResultState>(); IdentityResult identityResult = IdentityResult.Failed(); try { identityResult = await UserManager.ConfirmEmailAsync(userId, token); } catch (Exception ex) { accountResult.ResultState = ConfirmUserResultState.EmailNotConfirmed; HandleException(nameof(ConfirmUserAsync), ex, ref accountResult); return(accountResult); } if (identityResult.Succeeded && (await AddToPatientRoleAsync(userId)).Succeeded) { try { DubaiCultureUser user = await UserManager.FindByIdAsync(userId); //await CreateNewAvatarAsync(user, requestContext.HttpContext.Server); accountResult.Success = true; accountResult.ResultState = ConfirmUserResultState.UserConfirmed; } catch (Exception ex) { accountResult.ResultState = ConfirmUserResultState.AvatarNotCreated; HandleException(nameof(ConfirmUserAsync), ex, ref accountResult); return(accountResult); } } accountResult.Errors.AddNonNullRange(identityResult.Errors); return(accountResult); }
public object MapToCustomModel( DubaiCultureUser user, Type targetModelType, Dictionary <(string propertyName, Type propertyType), object> customMappings = null)
public async Task <DubaiCultureUser> FindUser(string userName, string password) { DubaiCultureUser user = await _userManager.FindAsync(userName, password); return(user); }
public async Task <DubaiCultureUser> GetUserByEmail(string email) { DubaiCultureUser user = await _userManager.FindByEmailAsync(email); return(user); }
public async Task <IdentityManagerResult <RegisterResultState> > RegisterAsync(RegisterViewModel uploadModel, bool emailConfirmed, RequestContext requestContext) { DubaiCultureUser user = new DubaiCultureUser { UserName = uploadModel.EmailViewModel.Email, Email = uploadModel.EmailViewModel.Email, FirstName = uploadModel.FirstName, LastName = uploadModel.LastName, Enabled = !emailConfirmed }; IdentityManagerResult <RegisterResultState> accountResult = new IdentityManagerResult <RegisterResultState>(); IdentityResult identityResult = null; try { identityResult = await UserManager.CreateAsync(user, uploadModel.PasswordConfirmationViewModel.Password); } catch (Exception ex) { HandleException(nameof(RegisterAsync), ex, ref accountResult); return(accountResult); } if (identityResult != null && identityResult.Succeeded) { // Registration: Confirmed registration (begin) if (emailConfirmed) { string token = null; try { token = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); } catch (Exception ex) { accountResult.ResultState = RegisterResultState.TokenNotCreated; HandleException(nameof(RegisterAsync), ex, ref accountResult); return(accountResult); } if (!string.IsNullOrEmpty(token)) { string confirmationUrl = new UrlHelper(requestContext).AbsoluteUrl( requestContext.HttpContext.Request, "ConfirmUser", routeValues: new { userId = user.Id, token }); await UserManager.SendEmailAsync(user.Id, Dependencies.LocalizationService.Localize("AccountManager.Register.Email.Confirm.Subject"), Dependencies.LocalizationService.LocalizeFormat("AccountManager.Register.Email.Confirm.Body", confirmationUrl)); accountResult.Success = true; accountResult.ResultState = RegisterResultState.EmailSent; } } // Registration: Confirmed registration (end) // Registration: Direct sign in (begin) else { identityResult = await AddToPatientRoleAsync(user.Id); try { //await CreateNewAvatarAsync(user, requestContext.HttpContext.Server); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); accountResult.ResultState = RegisterResultState.SignedIn; accountResult.Success = true; } catch (Exception ex) { accountResult.ResultState = RegisterResultState.NotSignedIn; HandleException(nameof(RegisterAsync), ex, ref accountResult); return(accountResult); } } // Registration: Direct sign in (end) } accountResult.Errors.AddNonNullRange(identityResult.Errors); return(accountResult); }
public async Task <IdentityManagerResult <ForgotPasswordResultState> > ForgotPasswordAsync(EmailViewModel uploadModel, RequestContext requestContext) { IdentityManagerResult <ForgotPasswordResultState> accountResult = new IdentityManagerResult <ForgotPasswordResultState>(); DubaiCultureUser user = null; try { user = await UserManager.FindByEmailAsync(uploadModel.Email); } catch (Exception ex) { accountResult.ResultState = ForgotPasswordResultState.UserNotFound; HandleException(nameof(ForgotPasswordAsync), ex, ref accountResult); return(accountResult); } // Registration: Confirmed registration (begin) if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { accountResult.ResultState = ForgotPasswordResultState.EmailNotConfirmed; return(accountResult); } // Registration: Confirmed registration (end) string token = null; try { token = await UserManager.GeneratePasswordResetTokenAsync(user.Id); } catch (Exception ex) { accountResult.ResultState = ForgotPasswordResultState.TokenNotCreated; HandleException(nameof(ForgotPasswordAsync), ex, ref accountResult); return(accountResult); } string resetUrl = new UrlHelper(requestContext).AbsoluteUrl( requestContext.HttpContext.Request, "ResetPassword", "Account", new { userId = user.Id, token }); try { await UserManager.SendEmailAsync(user.Id, Dependencies.LocalizationService.Localize("PassReset.Title"), Dependencies.LocalizationService.LocalizeFormat("AccountManager.ForgotPassword.Email.Body", resetUrl)); } catch (Exception ex) { accountResult.ResultState = ForgotPasswordResultState.EmailNotSent; HandleException(nameof(ForgotPasswordAsync), ex, ref accountResult); return(accountResult); } accountResult.Success = true; accountResult.ResultState = ForgotPasswordResultState.EmailSent; return(accountResult); }