婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)

ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)

熱門標(biāo)簽:辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作 安陸市地圖標(biāo)注app 寧德防封版電銷卡 西寧電銷外呼系統(tǒng)公司 聊城智能電銷機(jī)器人電話 海東防封電銷卡 上海市三維地圖標(biāo)注 云南外呼系統(tǒng)代理 南昌自動(dòng)外呼系統(tǒng)線路

本文實(shí)例為大家分享了ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷的具體方法,供大家參考,具體內(nèi)容如下

一、創(chuàng)建ClaimsIdentity

ClaimsIdentity(委托基于聲明的標(biāo)識(shí))是在ASP.NET Identity身份認(rèn)證系統(tǒng)的登錄時(shí)要用到,我們?cè)赨serService中來(lái)生成它。

1、打開IBLL項(xiàng)目InterfaceUserService接口,添加接口方法ClaimsIdentity CreateIdentity(User user, string authenticationType);

2、打開BLL項(xiàng)目的UserService類,添加CreateIdentity方法的實(shí)現(xiàn)代碼

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(認(rèn)證管理器)

打開Ninesky.Web項(xiàng)目 Member區(qū)域的UserController,添加AuthenticationManager屬性,在HttpContext.GetOwinContext()中獲取這個(gè)屬性。

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

三、創(chuàng)建登錄視圖模型

Member區(qū)域的模型文件夾添加視圖模型

using System.ComponentModel.DataAnnotations;

namespace Ninesky.Web.Areas.Member.Models
{
 /// summary>
 /// 登錄模型
 /// remarks>
 /// 創(chuàng)建:2014.02.16
 /// /remarks>
 /// /summary>
 public class LoginViewModel
 {
 /// summary>
 /// 用戶名
 /// /summary>
 [Required(ErrorMessage = "必填")]
 [StringLength(20, MinimumLength = 4, ErrorMessage = "{2}到{1}個(gè)字符")]
 [Display(Name = "用戶名")]
 public string UserName { get; set; }

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

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

四、創(chuàng)建登錄頁(yè)面

在UserCcontroller中添加(string returnUrl) action

/// summary>
 /// 用戶登錄
 /// /summary>
 /// param name="returnUrl">返回Url/param>
 /// returns>/returns>
 public ActionResult Login(string returnUrl)
 {
 return View();
 }

右鍵添加強(qiáng)類型視圖,模型為L(zhǎng)oginViewModel

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

@{
 ViewBag.Title = "會(huì)員登錄";
}

@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 
 div class="form-horizontal">
 h4>會(huì)員登錄/h4>
 hr />
 @Html.ValidationSummary(true)

 div class="form-group">
 @Html.LabelFor(model => model.UserName, new { @class = "control-label col-md-2" })
 div class="col-md-10">
 @Html.EditorFor(model => model.UserName)
 @Html.ValidationMessageFor(model => model.UserName)
 /div>
 /div>

 div class="form-group">
 @Html.LabelFor(model => model.Password, new { @class = "control-label col-md-2" })
 div class="col-md-10">
 @Html.EditorFor(model => model.Password)
 @Html.ValidationMessageFor(model => model.Password)
 /div>
 /div>

 div class="form-group">
 @Html.LabelFor(model => model.RememberMe, new { @class = "control-label col-md-2" })
 div class="col-md-10">
 @Html.EditorFor(model => model.RememberMe)
 @Html.ValidationMessageFor(model => model.RememberMe)
 /div>
 /div>

 div class="form-group">
 div class="col-md-offset-2 col-md-10">
 input type="submit" value="登錄" class="btn btn-default" />
 /div>
 /div>
 /div>
}

@section Scripts {
 @Scripts.Render("~/bundles/jqueryval")
}

效果

五、創(chuàng)建用戶登錄處理action

在UserCcontroller中添加 httppost類型的 Login action中先用ModelState.IsValid看模型驗(yàn)證是否通過(guò),沒(méi)通過(guò)直接返回,通過(guò)則檢查用戶密碼是否正確。用戶名密碼正確用CreateIdentity方法創(chuàng)建標(biāo)識(shí),然后用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", "密碼錯(cuò)誤");
 }
 return View();
 }

六、修改用戶注冊(cè)代碼

讓用戶注冊(cè)成功后直接登錄

七、注銷

在UserCcontroller中添加在Logout action

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

本文已被整理到了《ASP.NET MVC網(wǎng)站開發(fā)教程》,歡迎大家學(xué)習(xí)閱讀,更多內(nèi)容還可以參考ASP.NET MVC5網(wǎng)站開發(fā)專題學(xué)習(xí)。

本文主要是用到了ClaimsIdentity(基于聲明的標(biāo)識(shí))、AuthenticationManager的SignOut、SignIn方法。希望對(duì)大家實(shí)現(xiàn)用戶注冊(cè)和注銷有所幫助。

您可能感興趣的文章:
  • asp.net BasePage類+Session通用用戶登錄權(quán)限控制
  • ASP.NET中在一般處理程序中使用session的簡(jiǎn)單介紹
  • ASP.NET Session使用詳解
  • asp.net(c#)有關(guān) Session 操作的幾個(gè)誤區(qū)
  • ASP.NET登錄注冊(cè)頁(yè)面實(shí)現(xiàn)
  • 一款經(jīng)典的ajax登錄頁(yè)面 后臺(tái)asp.net
  • 基于.Net的單點(diǎn)登錄(SSO)實(shí)現(xiàn)解決方案
  • .net MVC使用Session驗(yàn)證用戶登錄(4)

標(biāo)簽:南寧 贛州 青海 衢州 洛陽(yáng) 汕尾 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)》,本文關(guān)鍵詞  ASP.NET,MVC5,網(wǎng)站開發(fā),用戶,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 深州市| 涪陵区| 夏邑县| 通渭县| 西和县| 崇明县| 乐亭县| 纳雍县| 巴南区| 遂宁市| 三都| 斗六市| 泸州市| 高尔夫| 霞浦县| 大化| 射洪县| 康乐县| 华坪县| 沙坪坝区| 长春市| 平舆县| 耒阳市| 镇巴县| 永善县| 丹阳市| 裕民县| 宁城县| 上犹县| 柘城县| 彰化市| 九龙坡区| 沧州市| 大同县| 锡林郭勒盟| 奉节县| 德江县| 馆陶县| 海城市| 平乐县| 陵水|