/// <summary> /// DELETE operation for a user. This is not currently used, but required by .NET Identity. /// </summary> /// <param name="user">The User object</param> /// <returns>Returns a 0 or 1 depending on whether operation is successful or not.</returns> public async Task DeleteAsync(User user) { await WithConnection(async connection => { string query = "DELETE FROM Users WHERE Id=@Id"; return await connection.ExecuteAsync(query, new { @Id = user.Id }); }); }
/// <summary> /// UPDATE operation for updating a user. /// </summary> /// <param name="user">The user that will be updated. The updated values must be passed in to this method.</param> /// <returns>Returns a 0 or 1 depending on whether operation is successful or not.</returns> public async Task UpdateAsync(User user) { await WithConnection(async connection => { string query = "UPDATE Users SET UserName=@UserName,Nickname=@Nickname,PasswordHash=@PasswordHash,SecurityStamp=@SecurityStamp,IsConfirmed=@IsConfirmed,CreatedDate=@CreatedDate,ConfirmationToken=@ConfirmationToken WHERE Id=@Id"; return await connection.ExecuteAsync(query, user); }); }
/// <summary> /// INSERT operation for a new user. /// </summary> /// <param name="user">The User object must be passed in. We create this during the Register Action.</param> /// <returns>Returns a 0 or 1 depending on whether operation is successful or not.</returns> public async Task CreateAsync(User user) { await WithConnection(async connection => { string query = "INSERT INTO Users(Id,UserName,Nickname,PasswordHash,SecurityStamp,IsConfirmed,ConfirmationToken,CreatedDate) VALUES(@Id,@UserName,@Nickname,@PasswordHash,@SecurityStamp,@IsConfirmed,@ConfirmationToken,@CreatedDate)"; user.Id = Guid.NewGuid().ToString(); return await connection.ExecuteAsync(query, user); }); }
/// <summary> /// Method for getting the security stamp for the user account. /// </summary> /// <param name="user">The user to get the security stamp for.</param> /// <returns>The security stamp.</returns> public Task<string> GetSecurityStampAsync(User user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } return Task.FromResult(user.SecurityStamp); }
/// <summary> /// Method for checking if an account has a password hash. /// </summary> /// <param name="user">The user to check for an existing password hash.</param> /// <returns>True of false depending on whether the password hash exists or not.</returns> public Task<bool> HasPasswordAsync(User user) { return Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash)); }
/// <summary> /// Method for setting the security stamp for the user account. /// </summary> /// <param name="user">The user to set the security stamp for.</param> /// <param name="stamp">The stamp to set.</param> /// <returns></returns> public Task SetSecurityStampAsync(User user, string stamp) { if (user == null) { throw new ArgumentNullException(nameof(user)); } user.SecurityStamp = stamp; return Task.FromResult(0); }
/// <summary> /// Method for setting the password hash for the user account. This hash is used to encode the users password. /// </summary> /// <param name="user">The user to has the password for.</param> /// <param name="passwordHash">The password has to use.</param> /// <returns></returns> public Task SetPasswordHashAsync(User user, string passwordHash) { if (user == null) { throw new ArgumentNullException(nameof(user)); } user.PasswordHash = passwordHash; return Task.FromResult(0); }
/// <summary> /// Method for getting teh password hash for the user account. /// </summary> /// <param name="user">The user to get the password hash for.</param> /// <returns>The password hash.</returns> public Task<string> GetPasswordHashAsync(User user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } return Task.FromResult(user.PasswordHash); }
/// <summary> /// DELETE operation for removing an external login from an existing user account. /// </summary> /// <param name="user">The user object that the external login will be removed from.</param> /// <param name="login">The external login that will be removed from the user account.</param> /// <returns>Returns a 0 or 1 depending on whether operation is successful or not.</returns> public async Task RemoveLoginAsync(User user, UserLoginInfo login) { await WithConnection(async connection => { string query = "DELETE FROM ExternalLogins WHERE Id = @Id AND LoginProvider = @loginProvider AND ProviderKey = @providerKey"; return await connection.ExecuteAsync(query, new { user.Id, login.LoginProvider, login.ProviderKey }); }); }
/// <summary> /// SELECT operation for getting external logins for a user account. /// </summary> /// <param name="user">The user account to get external login information for.</param> /// <returns>List of UserLoginInfo objects that contain external login information for each associated external account.</returns> public async Task<IList<UserLoginInfo>> GetLoginsAsync(User user) { return await WithConnection(async connection => { string query = "SELECT LoginProvider, ProviderKey FROM ExternalLogins WHERE UserId = @Id"; var loginInfo = await connection.QueryAsync<UserLoginInfo>(query, user); return loginInfo.ToList(); }); }
/// <summary> /// INSERT operation for adding an external login such as Google for a new or existing account. /// </summary> /// <param name="user">The User object that will be associated with the external login information.</param> /// <param name="login">The user login information. This object is constructed during the callback from the external authority.</param> /// <returns>Returns a 0 or 1 depending on whether operation is successful or not.</returns> public async Task AddLoginAsync(User user, UserLoginInfo login) { await WithConnection(async connection => { string query = "INSERT INTO ExternalLogins(ExternalLoginId, UserId, LoginProvider, ProviderKey) VALUES(@externalLoginId, @userId, @loginProvider, @providerKey)"; return await connection.ExecuteAsync(query, new { externalLoginId = Guid.NewGuid(), userId = user.Id, loginProvider = login.LoginProvider, providerKey = login.ProviderKey }); }); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { //Generate a new confirmation token. Here we are just storing a Guid as a string, but feel free to use whatever you want (if you use another type, make sure to update the user object //and the user table accordingly). var confirmationToken = Guid.NewGuid().ToString(); //Create the User object. If you have customized this beyond this example, make sure you update this to contain your new fields. //The confirmation token in our example is ultimately for show. Make sure to modify the RegisterViewModel and the Register view if you have customized the object. var user = new User { UserName = model.Email.TrimEnd(), Nickname = model.Nickname.TrimEnd(), IsConfirmed = true, ConfirmationToken = confirmationToken, CreatedDate = DateTime.UtcNow }; //Create the user var result = await _userManager.CreateAsync(user, model.Password); //If it's successful we log the user in and redirect to the home page if (result.Succeeded) { //send e-mail confirmation here and instead of logging the user in and taking them to the home page, redirect them to some page indicating a confirmation email has been sent to them await SignInAsync(user, false); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }
/// <summary> /// This method generates a new confirmation token, updates the stored confirmation token and then sends a new confirmation email to the user. /// </summary> /// <param name="user"></param> /// <returns></returns> private async Task ResendConfirmationToken(User user) { //create a new confirmation token var confirmationToken = Guid.NewGuid().ToString(); //update the users confirmation token and reset the created date user.ConfirmationToken = confirmationToken; user.CreatedDate = DateTime.UtcNow; await _userManager.UpdateAsync(user); //send the new confirmation link to the user //await EmailConfirmationHelper.SendRegistrationEmail(confirmationToken, user.UserName); }
private async Task SignInAsync(User user, bool isPersistent) { AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); }
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) { if (User.Identity.IsAuthenticated) { return RedirectToAction("Index", "Manage"); } if (ModelState.IsValid) { // Get the information about the user from the external login provider var info = await AuthenticationManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); } //here we can either use information we gathered with claims that will be contained in the info object, or we can use the data from the form - both is available to us. var user = new User { UserName = model.Email.TrimEnd(), Nickname = model.Nickname.TrimEnd(), CreatedDate = DateTime.UtcNow, IsConfirmed = true }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) { result = await _userManager.AddLoginAsync(user.Id, info.Login); if (result.Succeeded) { await SignInAsync(user, isPersistent: false); return RedirectToLocal(returnUrl); } } AddErrors(result); } ViewBag.ReturnUrl = returnUrl; return View(model); }