MVC4制作网站中怎样实现用户登陆
发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,本篇文章给大家分享的是有关MVC4制作网站中怎样实现用户登陆,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一用户1.1用户注册1.2用户
千家信息网最后更新 2024年09月30日MVC4制作网站中怎样实现用户登陆你注册时在注册页面停留的时间过久页已经超时 您绕开客户端验证向服务器提交数据 ", Solution = "返回注册页面,刷新后重新注册" }; return RedirectToAction("Error", "Prompt", _e); } else if (Session["VerificationCode"].ToString() != login.VerificationCode.ToUpper()) { ModelState.AddModelError("VerificationCode", "×"); return View(); } //验证账号密码 userRsy = new UserRepository(); if (userRsy.Authentication(login.UserName, Common.Text.Sha256(login.Password)) == 0) { HttpCookie _cookie = new HttpCookie("User"); _cookie.Values.Add("UserName", login.UserName); _cookie.Values.Add("Password", Common.Text.Sha256(login.Password)); Response.Cookies.Add(_cookie); return RedirectToAction("Default","User"); } else { ModelState.AddModelError("Message", "登陆失败!"); return View(); } }
本篇文章给大家分享的是有关MVC4制作网站中怎样实现用户登陆,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
一用户
1.1用户注册
1.2用户登陆
首先在Models里添加用户登陆模型类UserLogin,该类只要用用户名,密码和验证码三个字段。
////// 用户登陆模型 /// public class UserLogin { ////// 用户名 /// [Display(Name = "用户名", Description = "4-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 4, ErrorMessage = "×")] public string UserName { get; set; } ////// 密码 /// [Display(Name = "密码", Description = "6-20个字符。")] [Required(ErrorMessage = "×")] [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string Password { get; set; } ////// 验证码 /// [Display(Name = "验证码", Description = "请输入图片中的验证码。")] [Required(ErrorMessage = "×")] [StringLength(6, MinimumLength = 6, ErrorMessage = "×")] public string VerificationCode { get; set; } }
在UserController里添加Login action; 代码看如下:
public ActionResult Login() { return View(); } [HttpPost] public ActionResult Login(UserLogin login) { return View(); }
使用Cookie保存登陆账号,密码等信息,修改public ActionResult Login(UserLogin login)。修改完成代码如下:
[HttpPost] public ActionResult Login(UserLogin login) { //验证验证码 if (Session["VerificationCode"] == null || Session["VerificationCode"].ToString() == "") { Error _e = new Error { Title = "验证码不存在", Details = "在用户注册时,服务器端的验证码为空,或向服务器提交的验证码为空", Cause = "
在public ActionResult Login() 上右键添加强类型视图
完成后代的Login.cshtml
@model CMS.Models.UserLogin@{ ViewBag.Title = "用户登陆"; Layout = "~/Views/Shared/_Layout.cshtml";}@using (Html.BeginForm()) { @Html.ValidationSummary(true)}@section Scripts { @Scripts.Render("~/bundles/jqueryval") }
- 用户登陆
@Html.LabelFor(model => model.UserName):@Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) @Html.DisplayDescriptionFor(model => model.UserName) @Html.LabelFor(model => model.Password):@Html.PasswordFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) @Html.DisplayDescriptionFor(model => model.Password) 验证码:@Html.TextBoxFor(model => model.VerificationCode) @Html.ValidationMessageFor(model => model.VerificationCode) 换一张 @Html.ValidationMessage("Message");
浏览器中查看一下登陆页面
点下登陆测试一下。OK登陆成功
验证用户是否已经登陆,这块和权限验证一起从AuthorizeAttribute继承个自定义验证类
在项目里添加Extensions文件夹,添加一个类UserAuthorizeAttribute 继承自AuthorizeAttribute,重写AuthorizeCore方法用来实现用户是否已经登陆的验证,权限验证在写权限功能时在补充
using Ninesky.Repository;namespace System.Web.Mvc{ ////// 用户权限验证 /// public class UserAuthorizeAttribute :AuthorizeAttribute { ////// 核心【验证用户是否登陆】 /// /// ///protected override bool AuthorizeCore(HttpContextBase httpContext) { //检查Cookies["User"]是否存在 if (httpContext.Request.Cookies["User"] == null) return false; //验证用户名密码是否正确 HttpCookie _cookie = httpContext.Request.Cookies["User"]; string _userName = _cookie["UserName"]; string _password = _cookie["Password"]; httpContext.Response.Write("用户名:"+_userName); if (_userName == "" || _password == "") return false; UserRepository _userRsy = new UserRepository(); if (_userRsy.Authentication(_userName, _password) == 0) return true; else return false; } }}
以后只要在需要登陆后才能操作的Action或Controller上加[UserAuthorize]就可实现验证是否已经登录了。
退出功能,在UserController添加Logout Action
////// 退出系统 /// ///public ActionResult Logout() { if (Request.Cookies["User"] != null) { HttpCookie _cookie = Request.Cookies["User"]; _cookie.Expires = DateTime.Now.AddHours(-1); Response.Cookies.Add(_cookie); } Notice _n = new Notice { Title = "成功退出", Details = "您已经成功退出!", DwellTime = 5, NavigationName="网站首页", NavigationUrl = Url.Action("Index", "Home") }; return RedirectToAction("Notice", "Prompt", _n); }
以上就是MVC4制作网站中怎样实现用户登陆,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
验证
用户
登陆
密码
权限
用户名
网站
成功
服务器
页面
服务
制作网站
制作
代码
功能
字符
更多
模型
用户注册
知识
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
全球原料药数据库
什么是集中管理存储服务器
e福州垃圾服务器
数据库保存的密码是加密的
金现代软件开发跳槽到大公司
在银行软件开发工作好
富国互联网科技160630
温州计算机网络技术报名
税务网络安全周
事务服务器
晋州网络安全
宠物商品信息数据库
计算机网络技术就职岗位
数据库计算工龄年数
企业网络安全管理规定
网络安全文字如何写
软件开发部报销核算类别
做我的世界服务器
眼镜行业软件开发一般收费
软件开发优秀员工
小学生网络安全征文
网络安全小剧场小学生
vr软件开发专业是靠吃青春饭吗
企业代理服务器
本地域名怎么改成服务器
微软笔试软件开发
软件开发抢购软件
烟台软件开发企业
h2数据库创建多个
杭州大诚网络技术有限公司