本文整理汇总了C#中UserManager.RemovePasswordAsync方法的典型用法代码示例。如果您正苦于以下问题:C# UserManager.RemovePasswordAsync方法的具体用法?C# UserManager.RemovePasswordAsync怎么用?C# UserManager.RemovePasswordAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserManager
的用法示例。
在下文中一共展示了UserManager.RemovePasswordAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ChangePassword
public async Task<JsonData> ChangePassword(ChangePasswordModel changePass)
{
try
{
var db = new DataContext();
var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
userMan.UserValidator = new UserValidator<MyUser>(userMan)
{
AllowOnlyAlphanumericUserNames =
false
};
var user = await userMan.FindByIdAsync(User.Identity.GetUserId());
if (user == null) throw new Exception("please check your old password");
var newPassword = changePass.NewPassword;
var result = await userMan.RemovePasswordAsync(user.Id);
if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
return DataHelpers.ReturnJsonData(null, true, "Password changed successful");
}
catch (Exception e)
{
return DataHelpers.ExceptionProcessor(e);
}
}
示例2: Save
public async Task<WikiDownUser> Save(IPrincipal principal, UserManager<WikiDownUser> userManager)
{
var user = await userManager.FindByNameAsync(this.UserName);
var roles = this.GetRoles(principal, user);
if (user != null)
{
if (user.UserName == principal.Identity.Name)
{
var userAccessLevel = ArticleAccessHelper.GetAccessLevel(user.Roles);
if (userAccessLevel < ArticleAccessLevel.Admin)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
}
user.SetRoles(roles);
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
if (!string.IsNullOrWhiteSpace(this.Password))
{
await userManager.RemovePasswordAsync(user.Id);
await userManager.AddPasswordAsync(user.Id, this.Password);
}
await userManager.UpdateAsync(user);
WikiDownUserCacheHelper.Clear(user.UserName);
}
else
{
user = new WikiDownUser(this.UserName) { Roles = roles };
user.SetDisplayName(this.DisplayName);
user.SetEmail(this.Email);
await userManager.CreateAsync(user, this.Password);
}
return user;
}
示例3: Reset
public async Task<JsonData> Reset(UserViewModel model)
{
try
{
var db = new DataContext();
var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db));
userMan.UserValidator = new UserValidator<MyUser>(userMan)
{
AllowOnlyAlphanumericUserNames =
false
};
var user = await userMan.FindByEmailAsync(model.Email);
if (user == null) throw new Exception("please check the email address");
//todo: generate a unique password and email it to the user
var newPassword = user.FullName.Substring(2, 3) + user.PasswordHash.Substring(0, 5);
var result = await userMan.RemovePasswordAsync(user.Id);
if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors));
var result2 = await userMan.AddPasswordAsync(user.Id, newPassword);
if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors));
//todo: Email the new password to the user
return DataHelpers.ReturnJsonData(null, true, "A new password has been emailed to your email address");
}
catch (Exception e)
{
return DataHelpers.ExceptionProcessor(e);
}
}
示例4: ForgotPassword
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordModel model)
{
if (ModelState.IsValid)
{
// Fetch userID by email
DBservices dbs = new DBservices();
dbs = dbs.ReadFromDataBase(27, model.EmailAddress);
string userId = dbs.dt.Rows[0].ItemArray[0].ToString();
string userName = dbs.dt.Rows[0].ItemArray[1].ToString();
string userFname = dbs.dt.Rows[0].ItemArray[2].ToString();
// Generate an 8th digit long password
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var randomPassword = new String(stringChars);
UserManager<IdentityUser> UserManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>());
IdentityResult resultRem = await UserManager.RemovePasswordAsync(userId);
IdentityResult resultAdd = await UserManager.AddPasswordAsync(userId, randomPassword);
//Send a notification to the user
MailMessage mail = new MailMessage();
StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/Models/mailTemplates/forgotPasswordTemplate.html"));
string readFile = reader.ReadToEnd();
string StrContent = readFile;
StrContent = StrContent.Replace("[FirstName]", userFname);
StrContent = StrContent.Replace("[UserName]", userName);
StrContent = StrContent.Replace("[Password]", randomPassword);
mail.IsBodyHtml = true;
mail.To.Add(model.EmailAddress);
mail.Subject = "רוכבים לעבודה, איפוס סיסמא";
mail.Body = StrContent.ToString();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
return Ok();
}