一用户 
1.1用户注册 
1.2用户登陆 

首先在Models里添加用户登陆模型类UserLogin,该类只要用用户名,密码和验证码三个字段。 

/// <summary>
 /// 用户登陆模型
 /// </summary>
 public class UserLogin
 {
 /// <summary>
 /// 用户名
 /// </summary>
 [Display(Name = "用户名", Description = "4-20个字符。")]
 [Required(ErrorMessage = "×")]
 [StringLength(20, MinimumLength = 4, ErrorMessage = "×")]
 public string UserName { get; set; }
 /// <summary>
 /// 密码
 /// </summary>
 [Display(Name = "密码", Description = "6-20个字符。")]
 [Required(ErrorMessage = "×")]
 [StringLength(20, MinimumLength = 6, ErrorMessage = "×")]
 [DataType(DataType.Password)]
 public string Password { get; set; }
 /// <summary>
 /// 验证码
 /// </summary>
 [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 = "<li>你注册时在注册页面停留的时间过久页已经超时</li><li>您绕开客户端验证向服务器提交数据</li>", Solution = "返回<a href='" + Url.Action("Register", "User") + "'>注册</a>页面,刷新后重新注册" };
  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();
  }

 }

在public ActionResult Login() 上右键添加强类型视图

MVC4制作网站教程第二章 用户登陆2.2

完成后代的Login.cshtml 

@model CMS.Models.UserLogin

@{
 ViewBag.Title = "用户登陆";
 Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<div class="banner"> 
 <img src="/UploadFiles/2021-04-02/banner.jpg">

浏览器中查看一下登陆页面

MVC4制作网站教程第二章 用户登陆2.2

点下登陆测试一下。OK登陆成功 

验证用户是否已经登陆,这块和权限验证一起从AuthorizeAttribute继承个自定义验证类 

在项目里添加Extensions文件夹,添加一个类UserAuthorizeAttribute 继承自AuthorizeAttribute,重写AuthorizeCore方法用来实现用户是否已经登陆的验证,权限验证在写权限功能时在补充 

using Ninesky.Repository;

namespace System.Web.Mvc
{
 /// <summary>
 /// 用户权限验证
 /// </summary>
 public class UserAuthorizeAttribute :AuthorizeAttribute
 {
 /// <summary>
 /// 核心【验证用户是否登陆】
 /// </summary>
 /// <param name="httpContext"></param>
 /// <returns></returns>
 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 

/// <summary>
 /// 退出系统
 /// </summary>
 /// <returns></returns>
 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);
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。