public override async Task GrantResourceOwnerCredentials( OAuthGrantResourceOwnerCredentialsContext context) { // Retrieve user from database: var store = new MyUserStore(new ApplicationDbContext()); var user = await store.FindByEmailAsync(context.UserName); // Validate user/password: if (user == null || !store.PasswordIsValid(user, context.Password)) { context.SetError( "invalid_grant", "The user name or password is incorrect."); context.Rejected(); return; } // Add claims associated with this user to the ClaimsIdentity object: var identity = new ClaimsIdentity(context.Options.AuthenticationType); foreach (var userClaim in user.Claims) { identity.AddClaim(new Claim(userClaim.ClaimType, userClaim.ClaimValue)); } context.Validated(identity); }
//public MyUserManager(IQueryableUserStore<t_User,int> store) : base(store) public MyUserManager(MyUserStore store) : base(store) { this._store = store; //this. _maxPeriodForChangePassword = 1; //this._lockoutEnabled = true; //this._maxPeriodSignin = 1; }
private async Task LoadSharedKeyAndQrCodeUriAsync(MyUserStore user) { // Load the authenticator key & QR code URI to display on the form var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } SharedKey = FormatKey(unformattedKey); var email = await _userManager.GetEmailAsync(user); AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var user = new MyUserStore { UserName = Input.Email, Email = Input.Email, FirstName = Employee.FirstName, LastName = Employee.LastName, EmployeeNumber = Employee.EmployeeNumber }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
private Task<IEnumerable<Claim>> validationCallback(string userName, string password) { using (DbContext dbContext = MyCtx.Create()) using (MyUserStore userStore = new MyUserStore(dbContext)) using (MyUserManager userManager = new MyUserManager(userStore)) { var user = userManager.FindByName(userName); if (user == null) { return null; } if (!userManager.CheckPassword(user, password)) { return null; } ClaimsIdentity claimsIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); return Task.FromResult(claimsIdentity.Claims); } }
public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new MyUserStore { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public ActionResult Index(string Login, string Password) { if ((Login == "admin") && (Password == "admin")) { //Microsoft.AspNet.Identity.EntityFramework //Microsoft.Owin.Security; //Microsoft.AspNet.Identity var authMan = System.Web.HttpContext.Current.GetOwinContext().Authentication; var userStore = new MyUserStore(); var manager = new UserManager<IdentityUser>(userStore); var user = new IdentityUser() { UserName = "******" }; var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authMan.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity); return RedirectPermanent("/Admin/Home/Index"); } return RedirectToAction("Index"); }