日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(2)用户登录、注销

發(fā)布時間:2023/12/13 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(2)用户登录、注销 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上次實現(xiàn)了用戶注冊,這次來實現(xiàn)用戶登錄,用到IAuthenticationManager的SignOut、SignIn方法和基于聲明的標(biāo)識。最后修改用戶注冊代碼實現(xiàn)注冊成功后直接登錄。

目錄:

ASP.NET MVC5 網(wǎng)站開發(fā)實踐 - 概述

ASP.NET MVC5 網(wǎng)站開發(fā)實踐(一) - 項目框架

ASP.NET MVC5 網(wǎng)站開發(fā)實踐(一) - 框架(續(xù)) 模型、數(shù)據(jù)存儲、業(yè)務(wù)邏輯

ASP.NET MVC5 網(wǎng)站開發(fā)實踐(二) - 用戶部分(1)用戶注冊

一、創(chuàng)建ClaimsIdentity

ClaimsIdentity(委托基于聲明的標(biāo)識)是在ASP.NET Identity身份認(rèn)證系統(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(認(rèn)證管理器)

打開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)建標(biāo)識,然后用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("~/"));}

八、總結(jié)

主要是用到了ClaimsIdentity(基于聲明的標(biāo)識)、AuthenticationManager的SignOut、SignIn方法。

代碼 Ninesky二.2.rar:http://pan.baidu.com/s/1jGI9e66

轉(zhuǎn)載于:https://www.cnblogs.com/mzwhj/p/3553376.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(2)用户登录、注销的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。