千家信息网

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

发表于:2025-02-12 作者:千家信息网编辑
千家信息网最后更新 2025年02月12日,本篇内容介绍了"如何编写ASP.NET MVC5网站开发用户登录、注销"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读
千家信息网最后更新 2025年02月12日如何编写ASP.NET MVC5网站开发用户登录、注销

本篇内容介绍了"如何编写ASP.NET MVC5网站开发用户登录、注销"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、创建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")}

效果

五、创建用户登录处理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(); }

六、修改用户注册代码

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

七、注销

在UserCcontroller中添加在Logout action

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

"如何编写ASP.NET MVC5网站开发用户登录、注销"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

登录 用户 模型 密码 方法 用户名 网站 属性 视图 项目 网站开发 开发 代码 会员 内容 区域 字符 接口 更多 标识 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全知识进课堂活动的目的 辛集软件开发自学网哪个好 服务器机柜静音方案 怎么删除数据库中某字段 软件开发人力资源外包公司 软件开发职责概述 成都360网络安全比赛 网络技术3 2院考试题 重庆银行软件开发招聘信息 个人信息网络安全承诺书 软件开发的hr想要什么 软件开发中软件过程的三要素 全境封锁2服务器目前无法使用 美国政府开源数据库简介 北京移动首选dns服务器 建立网络安全工作保障机制 自学网络安全要多长时间 java枚举 数据库 网络安全与执法能去公司吗 湖南软件开发娱敞v岚鸿 数据库技术主要与应用 进口串口通信服务器公司 数据库从高到低排序输出 软件开发专业最高级证书 软件开发专科毕业后端工资 qq数据库 读取 软件开发部门业绩 云顶之弈美测试服服务器维护 数据库怎么使用语句表 宣传网络安全法与党建活动
0