如何编写ASP.NET MVC5网站开发用户登录、注销
创始人
2024-11-28 19:22:13
0次

一、创建ClaimsIdentity

ClaimsIdentity(委托基于声明的标识)是在ASP.NET Identity身份认证系统的登录时要用到,我们在UserService中来生成它。

1、打开IBLL项目InterfaceUserService接口,添加接口方法ClaimsIdentity CreateIdentity(User user, string authenticationType);

2、打开BLL项目的UserService类,添加CreateIdentity方法的实现代码

public ClaimsIdentity CreateIdentity(User user, string authenticationType)
 {
 ClaimsIdentity _identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
 _identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
 _identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString()));
 _identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity"));
 _identity.AddClaim(new Claim("DisplayName", user.DisplayName));
 return _identity;
 }

二、获取AuthenticationManager(认证管理器)

打开Ninesky.Web项目 Member区域的UserController,添加AuthenticationManager属性,在HttpContext.GetOwinContext()中获取这个属性。

#region 属性
 private IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } }
 #endregion

三、创建登录视图模型

Member区域的模型文件夹添加视图模型

using System.ComponentModel.DataAnnotations;

namespace Ninesky.Web.Areas.Member.Models
{
 /// 
 /// 登录模型
 /// 
 /// 创建:2014.02.16
 /// 
 /// 
 public class LoginViewModel
 {
 /// 
 /// 用户名
 /// 
 [Required(ErrorMessage = "必填")]
 [StringLength(20, MinimumLength = 4, ErrorMessage = "{2}到{1}个字符")]
 [Display(Name = "用户名")]
 public string UserName { get; set; }

 /// 
 /// 密码
 /// 
 [Required(ErrorMessage = "必填")]
 [Display(Name = "密码")]
 [StringLength(20, MinimumLength = 6, ErrorMessage = "{2}到{1}个字符")]
 [DataType(DataType.Password)]
 public string Password { get; set; }

 /// 
 /// 记住我
 /// 
 [Display(Name = "记住我")]
 public bool RememberMe { get; set; }
 }
}

四、创建登录页面

在UserCcontroller中添加(string returnUrl) action

/// 
 /// 用户登录
 /// 
 /// 返回Url
 /// 
 public ActionResult Login(string returnUrl)
 {
 return View();
 }

右键添加强类型视图,模型为LoginViewModel

@model Ninesky.Web.Areas.Member.Models.LoginViewModel

@{
 ViewBag.Title = "会员登录";
}

@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 
 
 
会员登录
   @Html.ValidationSummary(true)    @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })    @Html.EditorFor(model => model.UserName)  @Html.ValidationMessageFor(model => model.UserName)  
 
   @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })    @Html.EditorFor(model => model.Password)  @Html.ValidationMessageFor(model => model.Password)  
 
   @Html.LabelFor(model => model.RememberMe, new { @class = "control-label col-md-2" })    @Html.EditorFor(model => model.RememberMe)  @Html.ValidationMessageFor(model => model.RememberMe)  
 
       
 
 
} @section Scripts {  @Scripts.Render("~/bundles/jqueryval") }

效果

如何编写ASP.NET MVC5网站开发用户登录、注销

五、创建用户登录处理action

在UserCcontroller中添加 httppost类型的 Login action中先用ModelState.IsValid看模型验证是否通过,没通过直接返回,通过则检查用户密码是否正确。用户名密码正确用CreateIdentity方法创建标识,然后用SignOut方法清空Cookies,然后用SignIn登录。

[ValidateAntiForgeryToken]
 [HttpPost]
 public ActionResult Login(LoginViewModel loginViewModel)
 {
 if(ModelState.IsValid)
 {
 var _user = userService.Find(loginViewModel.UserName);
 if (_user == null) ModelState.AddModelError("UserName", "用户名不存在");
 else if (_user.Password == Common.Security.Sha256(loginViewModel.Password))
 {
 var _identity = userService.CreateIdentity(_user, DefaultAuthenticationTypes.ApplicationCookie);
 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
 AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = loginViewModel.RememberMe }, _identity);
 return RedirectToAction("Index", "Home");
 }
 else ModelState.AddModelError("Password", "密码错误");
 }
 return View();
 }

六、修改用户注册代码

让用户注册成功后直接登录

如何编写ASP.NET MVC5网站开发用户登录、注销

七、注销

在UserCcontroller中添加在Logout action

/// 
 /// 登出
 /// 
 /// 
 public ActionResult Logout()
 {
 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
 return Redirect(Url.Content("~/"));
 }

上一篇:ASP.NET MVC5中怎么修改用户资料和密码

下一篇:如何实现ASP.NET MVC5网站开发用户注册

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...