冯 海

一个程序新人菜鸟的日记,希望大家多多关照。QQ:32316131

ASP.NET Identity教程二:(用户管理5)通过后台页面中直接重置用户密码,获取identity的code值

一、在LIST前台增加链接

	@Html.ActionLink("重置", "ResetPassword", new { id = user.Id }, new { @class = "btn btn-warning" })

 二,新建一个重置秘码的模型ResetPasswordViewModel

using System.ComponentModel.DataAnnotations;

namespace jsdhh2.ViewModels
{
	public class ResetPasswordViewModel
	{
		[Required]
		[EmailAddress]
		[Display(Name = "电子邮件")]
		public string Email { get; set; }

		[Required]
		[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
		[DataType(DataType.Password)]
		[Display(Name = "密码")]
		public string Password { get; set; }

		[DataType(DataType.Password)]
		[Display(Name = "确认密码")]
		[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
		public string ConfirmPassword { get; set; }

		public string Code { get; set; }
	}
}

 3.在UserControll中增加方法

/// <summary>
		/// 重置密码
		/// </summary>
		/// <param name="code"></param>
		/// <returns></returns>
		[HttpGet]
		[AllowAnonymous]
		public ActionResult ResetPassword(string id)
		{
			if (string.IsNullOrWhiteSpace(id))
			{
				return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
			}
			User user = UserManager.FindById(id);
			string code = UserManager.GeneratePasswordResetToken(id);
			if (user == null)
			{
				return HttpNotFound();
			}
			var resetPasswordViewModel = new jsdhh2.ViewModels.ResetPasswordViewModel()
			{

				Email = user.Email,
				Password = user.PasswordHash,
				Code = code
				 
			};

		
			return View(resetPasswordViewModel);
		}

		/// <summary>
		/// 重置密码
		/// </summary>
		/// <param name="model"></param>
		/// <returns></returns>
		[HttpPost]
		[AllowAnonymous]
		[ValidateAntiForgeryToken]
		public async Task<ActionResult> ResetPassword(string id,jsdhh2.ViewModels.ResetPasswordViewModel resetPasswordViewModel)
		{
			//if (!ModelState.IsValid)
			//{
			//	return View(resetPasswordViewModel);
			//}
			 
          //User user = UserManager.FindById(id);
			string code = await UserManager.GeneratePasswordResetTokenAsync(id);


			//if (user == null)
			//{
			//	return View("Error");
			//}
			resetPasswordViewModel.Code = code;
			//resetPasswordViewModel.Password = "111111";

			var result = await UserManager.ResetPasswordAsync(id, resetPasswordViewModel.Code, resetPasswordViewModel.Password);
			if (result.Succeeded)
			{
				ViewBag.RPWMessagess = "重置成功";
				return RedirectToAction("List", "User");
			}
			AddErrors(result);
			ViewBag.RPWMessagess = "失败";
			return View("成功重置。");
		}

  

 

4,增加一个ResetPassword视图

@model jsdhh2.ViewModels.ResetPasswordViewModel
@{
	ViewBag.Title = "重置密码";
}

@using (Html.BeginForm("ResetPassword", "User", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
	@Html.AntiForgeryToken()
	<h4>重置你的密码。</h4>
	<hr />
	@Html.ValidationSummary("", new { @class = "text-danger" })
	@Html.HiddenFor(model => model.Code)
	@*<div class="form-group">
		@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
		</div>
	</div>*@
	<div class="form-group">
		@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		<div class="col-md-offset-2 col-md-10">
			<input type="submit" class="btn btn-default" value="重置" />
		</div>
	</div>
	@ViewBag.RPWMessagess
}

  

 

5.运行效果

 

 

 

 

 

 

连续输入二次,成功重置。

 

posted @ 2017-05-15 00:21  秋天来了哟  阅读(322)  评论(0编辑  收藏  举报
认识就是缘份,愿天下人都快乐!
QQ: 32316131
Email: 32316131@qq.com