欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年04月27日 13:47:05   投稿:lijiao  
這篇文章主要介紹了ASP.NET?MVC5?網(wǎng)站開發(fā)中用戶登錄、注銷的實現(xiàn)功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

一、創(chuàng)建ClaimsIdentity

ClaimsIdentity(委托基于聲明的標識)是在ASP.NET Identity身份認證系統(tǒng)的登錄時要用到,我們在UserService中來生成它。

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

2、打開BLL項目的UserService類,添加CreateIdentity方法的實現(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(認證管理器)

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

#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}個字符")]
 [Display(Name = "用戶名")]
 public string UserName { get; set; }

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

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

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

在UserCcontroller中添加(string returnUrl) action

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

右鍵添加強類型視圖,模型為LoginViewModel

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

@{
 ViewBag.Title = "會員登錄";
}

@using (Html.BeginForm()) 
{
 @Html.AntiForgeryToken()
 
 <div class="form-horizontal">
 <h4>會員登錄</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看模型驗證是否通過,沒通過直接返回,通過則檢查用戶密碼是否正確。用戶名密碼正確用CreateIdentity方法創(chuàng)建標識,然后用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

/// <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(基于聲明的標識)、AuthenticationManager的SignOut、SignIn方法。希望對大家實現(xiàn)用戶注冊和注銷有所幫助。

相關(guān)文章

  • asp.net中SqlCacheDependency緩存技術(shù)概述

    asp.net中SqlCacheDependency緩存技術(shù)概述

    這篇文章主要介紹了asp.net中SqlCacheDependency緩存技術(shù)概述,是大型web程序設(shè)計中常用的技術(shù),本文對此進行了較為詳細的描述,需要的朋友可以參考下
    2014-08-08
  • ASP.NET中GridView的文件輸出流方式

    ASP.NET中GridView的文件輸出流方式

    本文的主要內(nèi)容是講ASP.NET中GridView輸出顯示的文件,這是個人項目中的一點小經(jīng)驗,希望能給到有需要幫助的人。
    2016-08-08
  • ASP.NET MVC下自定義錯誤頁和展示錯誤頁的方式

    ASP.NET MVC下自定義錯誤頁和展示錯誤頁的方式

    這篇文章主要為大家詳細介紹了ASP.NET MVC下自定義錯誤頁和展示錯誤頁的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • asp.net neatUpload 支持大文件上傳組件

    asp.net neatUpload 支持大文件上傳組件

    Brettle.Web.NeatUpload.dll,可以看到工具箱中出現(xiàn)InputFile等控件
    2009-04-04
  • .NET Core 2.0如何生成圖片驗證碼完整實例

    .NET Core 2.0如何生成圖片驗證碼完整實例

    這篇文章主要給大家介紹了關(guān)于.NET Core 2.0如何生成圖片驗證碼的相關(guān)資料,該功能主要是利用ZKWeb.System.Drawing來實現(xiàn),文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Asp.net自定義控件之單選、多選控件

    Asp.net自定義控件之單選、多選控件

    這篇文章主要為大家詳細介紹了Asp.net自定義控件之單選、多選控件的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06
  • asp.net 簡單工廠模式和工廠方法模式之論述

    asp.net 簡單工廠模式和工廠方法模式之論述

    簡單工廠模式的最大優(yōu)點在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實例化相關(guān)的類,對于客戶端來說,去除了于具體產(chǎn)品的依賴
    2011-12-12
  • MVC分頁之MvcPager使用詳解

    MVC分頁之MvcPager使用詳解

    這篇文章主要為大家詳細介紹了MVC分頁之MvcPager使用方法,針對MvcPager同步和Ajax異步分頁進行講解,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Asp.Net Core Identity 隱私數(shù)據(jù)保護的實現(xiàn)

    Asp.Net Core Identity 隱私數(shù)據(jù)保護的實現(xiàn)

    這篇文章主要介紹了Asp.Net Core Identity 隱私數(shù)據(jù)保護的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • asp.net 用繼承方法實現(xiàn)頁面判斷session

    asp.net 用繼承方法實現(xiàn)頁面判斷session

    在做ASP項目的時候,判斷用戶是否登陸常用的方法是在每個頁面判斷session是否存在,無奈用java的時候過濾器就用的不熟。。。還是用繼承吧。汗。。。
    2009-09-09

最新評論