asp.net core 2.0的認證和授權

来源:http://www.cnblogs.com/axzxs2001/archive/2017/09/06/7482771.html
-Advertisement-
Play Games

在asp.net core中,微軟提供了基於認證(Authentication)和授權(Authorization)的方式,來實現許可權管理的,本篇博文,介紹基於固定角色的許可權管理和自定義角色許可權管理,本文內容,更適合傳統行業的BS應用,而非互聯網應用。 ...


在asp.net core中,微軟提供了基於認證(Authentication)和授權(Authorization)的方式,來實現許可權管理的,本篇博文,介紹基於固定角色的許可權管理和自定義角色許可權管理,本文內容,更適合傳統行業的BS應用,而非互聯網應用。

在asp.net core中,我們認證(Authentication)通常是在Login的Post Action中進行用戶名或密碼來驗證用戶是否正確,如果通過驗證,即該用戶就會獲得一個或幾個特定的角色,通過ClaimTypes.Role來存儲角色,從而當一個請求到達時,用這個角色和Controller或Action上加的特性 [Authorize(Roles = "admin,system")]來授權是否有權訪問該Action。本文中的自定義角色,會把驗證放在中間件中進行處理。

 一、固定角色:

即把角色與具體的Controller或Action直接關聯起來,整個系統中的角色是固定的,每種角色可以訪問那些Controller或Action也是固定的,這做法比較適合小型項目,角色分工非常明確的項目。

項目代碼:

https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86/RolePrivilegeManagement

始於startup.cs

需要在ConfigureServices中註入Cookie的相關信息,options是CookieAuthenticationOptions,關於這個類型提供如下屬性,可參考:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x

 

它提供了登錄的一些信息,或登錄生成Cookie的一些信息,用以後

 1         public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddMvc();
 4             //添加認證Cookie信息
 5             services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
 6              .AddCookie(options =>
 7              {
 8                  options.LoginPath = new PathString("/login");
 9                  options.AccessDeniedPath = new PathString("/denied");
10              });
11         }
12 
13         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
14         {
15             if (env.IsDevelopment())
16             {
17                 app.UseDeveloperExceptionPage();
18                 app.UseBrowserLink();
19             }
20             else
21             {
22                 app.UseExceptionHandler("/Home/Error");
23             }
24             app.UseStaticFiles();
25             //驗證中間件
26             app.UseAuthentication();
27             app.UseMvc(routes =>
28             {
29                 routes.MapRoute(
30                     name: "default",
31                     template: "{controller=Home}/{action=Index}/{id?}");
32             });
33         }

HomeController.cs

對於Login Get的Action,把returnUrl用戶想要訪問的地址(有可能用戶記錄下想要訪問的url了,但系統會轉到登錄頁,登錄成功後直接跳轉到想要訪問的returnUrl頁)

對於Login Post的Action,驗證用戶密和密碼,成功能,定義一個ClaimsIdentity,把用戶名和角色,和用戶姓名的聲明都添回進來(這個角色,就是用來驗證可訪問action的角色 )作來該用戶標識,接下來調用HttpContext.SignInAsync進行登錄,註意此方法的第一個參數,必需與StartUp.cs中services.AddAuthentication的參數相同,AddAuthentication是設置登錄,SigninAsync是按設置參數進行登錄

對於Logout Get的Action,是退出登錄

HomeController上的[Authorize(Roles=”admin,system”)]角色和許可權的關係時,所有Action只有admin和system兩個角色能訪問到,About上的[Authorize(Roles=”admin”)]聲明這個action只能admin角色訪問,Contact上的[Authorize(Roles=”system”)]聲明這個action只能system角色訪問,如果action上聲明的是[AllowAnomymous],說明不受授權管理,可以直接訪問。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Mvc;
 7 using RolePrivilegeManagement.Models;
 8 using System.Security.Claims;
 9 using Microsoft.AspNetCore.Authentication;
10 using Microsoft.AspNetCore.Authentication.Cookies;
11 using Microsoft.AspNetCore.Authorization;
12 
13 namespace RolePrivilegeManagement.Controllers
14 {
15     [Authorize(Roles = "admin,system")]
16     public class HomeController : Controller
17     {
18         public IActionResult Index()
19         {
20             return View();
21         }
22         [Authorize(Roles = "admin")]
23         public IActionResult About()
24         {
25             ViewData["Message"] = "Your application description page.";
26             return View();
27         }
28         [Authorize(Roles = "system")]
29         public IActionResult Contact()
30         {
31             ViewData["Message"] = "Your contact page.";
32             return View();
33         }
34         public IActionResult Error()
35         {
36             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
37         }
38         [AllowAnonymous]
39         [HttpGet("login")]
40         public IActionResult Login(string returnUrl = null)
41         {
42             TempData["returnUrl"] = returnUrl;
43             return View();
44         }
45         [AllowAnonymous]
46         [HttpPost("login")]
47         public async Task<IActionResult> Login(string userName, string password, string returnUrl = null)
48         {
49             var list = new List<dynamic> {
50                 new { UserName = "gsw", Password = "111111", Role = "admin" },
51                 new { UserName = "aaa", Password = "222222", Role = "system" }
52             };
53             var user = list.SingleOrDefault(s => s.UserName == userName && s.Password == password);
54             if (user!=null)
55             {
56                 //用戶標識
57                 var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
58                 identity.AddClaim(new Claim(ClaimTypes.Sid, userName));
59                 identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
60                 identity.AddClaim(new Claim(ClaimTypes.Role, user.Role));
61                 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
62                 if (returnUrl == null)
63                 {
64                     returnUrl = TempData["returnUrl"]?.ToString();
65                 }
66                 if (returnUrl != null)
67                 {
68                     return Redirect(returnUrl);
69                 }
70                 else
71                 {
72                     return RedirectToAction(nameof(HomeController.Index), "Home");
73                 }
74             }
75             else
76             {
77                 const string badUserNameOrPasswordMessage = "用戶名或密碼錯誤!";
78                 return BadRequest(badUserNameOrPasswordMessage);
79             }
80         }
81         [HttpGet("logout")]
82         public async Task<IActionResult> Logout()
83         {
84             await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
85             return RedirectToAction("Index", "Home");
86         }
87         [AllowAnonymous]
88         [HttpGet("denied")]
89         public IActionResult Denied()
90         {
91             return View();
92         }
93     }
94 }

前端_Layout.cshtml佈局頁,在登錄成功後的任何頁面都可以用@User.Identity.Name就可以獲取用戶姓名,同時用@User.Claims.SingleOrDefault(s=>s.Type== System.Security.Claims.ClaimTypes.Sid).Value可以獲取用戶名或角色。

 1    <nav class="navbar navbar-inverse navbar-fixed-top">
 2         <div class="container">
 3             <div class="navbar-header">
 4                 <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
 5                     <span class="sr-only">Toggle navigation</span>
 6                     <span class="icon-bar"></span>
 7                     <span class="icon-bar"></span>
 8                     <span class="icon-bar"></span>
 9                 </button>
10                 <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">RolePrivilegeManagement</a>
11             </div>
12             <div class="navbar-collapse collapse">
13                 <ul class="nav navbar-nav">
14                     <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
15                     <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
16                     <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
17                 </ul>
18                 <ul class="" style="float:right; margin:0;">
19                     <li style="overflow:hidden;">
20                         <div style="float:left;line-height:50px;margin-right:10px;">
21                             <span style="color:#ffffff">當前用戶:@User.Identity.Name</span>
22                         </div>
23                         <div style="float:left;line-height:50px;">
24                             <a asp-area="" asp-controller="Home" asp-action="Logout">註銷</a>
25                         </div>
26                     </li>
27                 </ul>
28             </div>
29         </div>
30     </nav>

現在可以用chrome運行了,進行登錄頁後F12,查看Network—Cookies,可以看到有一個Cookie,這個是記錄returnUrl的Cookie,是否記得HomeController.cs中的Login Get的Action中代碼:TempData["returnUrl"] = returnUrl;這個TempData最後轉成了一個Cookie返回到客戶端了,如下圖:

輸入用戶名,密碼登錄,再次查看Cookies,發現多了一個.AspNetCore.Cookies,即把用戶驗證信息加密碼保存在了這個Cookie中,當跳轉到別的頁面時,這兩個Cookie會繼續在客戶端和服務傳送,用以驗證用戶角色。

二、自定義角色

系統的角色可以自定義,用戶是自寫到義,許可權是固定的,角色對應許可權可以自定義,用戶對應角色也是自定義的,如下圖:

項目代碼:

https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/%E6%9D%83%E9%99%90%E7%AE%A1%E7%90%86/PrivilegeManagement

始於startup.cs

自定義角色與固定角色不同之處在於多了一個中間件(關於中間件學習參看:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware),即在Configure方法中,一定要在app.UseAuthentication下麵添加驗證許可權的中間件,因為UseAuthentication要從Cookie中載入通過驗證的用戶信息到Context.User中,所以一定放在載入完後才能去驗用戶信息(當然自己讀取Cookie也可以)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Builder;
 6 using Microsoft.AspNetCore.Hosting;
 7 using Microsoft.Extensions.Configuration;
 8 using Microsoft.Extensions.DependencyInjection;
 9 using Microsoft.AspNetCore.Authentication.Cookies;
10 using Microsoft.AspNetCore.Http;
11 using PrivilegeManagement.Middleware;
12 
13 namespace PrivilegeManagement
14 {
15     public class Startup
16     {
17         public Startup(IConfiguration configuration)
18         {
19             Configuration = configuration;
20         }
21         public IConfiguration Configuration { get; }
22 
23         public void ConfigureServices(IServiceCollection services)
24         {
25             services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
26            .AddCookie(options =>
27            {
28                options.LoginPath = new PathString("/login");
29                options.AccessDeniedPath = new PathString("/denied");
30            }
31            );
32             services.AddMvc();
33         }
34 
35         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
36         {
37             if (env.IsDevelopment())
38             {
39                 app.UseDeveloperExceptionPage();
40                 app.UseBrowserLink();
41             }
42             else
43             {
44                 app.UseExceptionHandler("/Home/Error");
45             }
46 
47             app.UseStaticFiles();
48             //驗證中間件
49             app.UseAuthentication();
50             ////添加許可權中間件, 一定要放在app.UseAuthentication後
51             app.UsePermission(new PermissionMiddlewareOption()
52             {
53                 LoginAction = @"/login",
54                 NoPermissionAction = @"/denied",
55                 //這個集合從資料庫中查出所有用戶的全部許可權
56                 UserPerssions = new List<UserPermission>()
57                  {
58                      new UserPermission { Url="/", UserName="gsw"},
59                      new UserPermission { Url="/home/contact", UserName="gsw"},
60                      new UserPermission { Url="/home/about", UserName="aaa"},
61                      new UserPermission { Url="/", UserName="aaa"}
62                  }
63             });
64             app.UseMvc(routes =>
65             {
66                 routes.MapRoute(
67                     name: "default",
68                     template: "{controller=Home}/{action=Index}/{id?}");
69             });
70         }
71     }
72 }

下麵看看中間件PermissionMiddleware.cs,在Invoke中用了context.User,如上面所述,首先要調用app.UseAuthentication載入用戶信息後才能在這裡使用,這個中間件邏輯較簡單,如果沒有驗證的一律放過去,不作處理,如果驗證過(登錄成功了),就要查看本次請求的url和這個用戶可以訪問的許可權是否匹配,如不匹配,就跳轉到拒絕頁面(這個是在Startup.cs中添加中間件時,用NoPermissionAction = @"/denied"設置的)

 1 using Microsoft.AspNetCore.Http;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Reflection;
 7 using System.Security.Claims;
 8 using System.Threading.Tasks;
 9 
10 namespace PrivilegeManagement.Middleware
11 {
12     /// <summary>
13     /// 許可權中間件
14     /// </summary>
15     public class PermissionMiddleware
16     {
17         /// <summary>
18         /// 管道代理對象
19         /// </summary>
20         private readonly RequestDelegate _next;
21         /// <summary>
22         /// 許可權中間件的配置選項
23         /// </summary>
24         private readonly PermissionMiddlewareOption _option;
25 
26         /// <summary>
27         /// 用戶許可權集合
28         /// </summary>
29         internal static List<UserPermission> _userPermissions;
30 
31         /// <summary>
32         /// 許可權中間件構造
33         /// </summary>
34         /// <param name="next">管道代理對象</param>
35         /// <param name="permissionResitory">許可權倉儲對象</param>
36         /// <param name="option">許可權中間件配置選項</param>
37         public PermissionMiddleware(RequestDelegate next, PermissionMiddlewareOption option)
38         {
39             _option = option;
40             _next = next;
41             _userPermissions = option.UserPerssions;
42         }       
43         /// <summary>
44         /// 調用管道
45         /// </summary>
46         /// <param name="context">請求上下文</param>
47         /// <returns></returns>
48         public Task Invoke(HttpContext context)
49         {
50             //請求Url
51             var questUrl = context.Request.Path.Value.ToLower();
52        
53             //是否經過驗證
54             var isAuthenticated = context.User.Identity.IsAuthenticated;
55             if (isAuthenticated)
56             {
57                 if (_userPermissions.GroupBy(g=>g.Url).Where(w => w.Key.ToLower() == questUrl).Count() > 0)
58                 {
59                     //用戶名
60                     var userName = context.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Sid).Value;
61                     if (_userPermissions.Where(w => w.UserName == userName&&w.Url.ToLower()==questUrl).Count() > 0)
62                     {
63                         return this._next(context);
64                     }
65                     else
66                     {
67                         //無許可權跳轉到拒絕頁面
68                         context.Response.Redirect(_option.NoPermissionAction);
69                     }
70                 }
71             }
72             return this._next(context);
73         }
74     }
75 }

擴展中間件類PermissionMiddlewareExtensions.cs

 1 using Microsoft.AspNetCore.Builder;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 
 7 namespace PrivilegeManagement.Middleware
 8 {
 9     /// <summary>
10     /// 擴展許可權中間件
11     /// </summary>
12     public static class PermissionMiddlewareExtensions
13     {
14         /// <summary>
15         /// 引入許可權中間件
16         /// </summary>
17         /// <param name="builder">擴展類型</param>
18         /// <param name="option">許可權中間件配置選項</param>
19         /// <returns></returns>
20         public static IApplicationBuilder UsePermission(
21               this IApplicationBuilder builder, PermissionMiddlewareOption option)
22         {
23             return builder.UseMiddleware<PermissionMiddleware>(option);
24         }
25     }
26 }

中間件屬性PermissionMiddlewareOption.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 
 6 namespace PrivilegeManagement.Middleware
 7 {
 8     /// <summary>
 9     /// 許可權中間件選項
10     /// </summary>
11     public class PermissionMiddlewareOption
12     {
13         /// <summary>
14         /// 登錄action
15         /// </summary>
16         public string LoginAction
17         { get; set; }
18         /// <summary>
19         /// 無許可權導航action
20         /// </summary>
21         public string NoPermissionAction
22         { get; set; }
23 
24         /// <summary>
	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 簡單來說內網穿透的目的是:讓外網能訪問你本地的應用,例如在外網打開你本地http://127.0.0.1指向的Web站點。 最近公司的花生殼到期了,要續費,發現價格一直在漲,都是5年以上的老用戶,旗艦版都沒有實現內網完全穿透,打算自己動手替換這個服務,中間走了不少的彎路,這裡記錄一些文字為大家提供參 ...
  • system定義 首先要知道,system函數是c庫中的函數,而不是系統調用。其實system函數使用起來並不複雜,難就難在對其返回值的理解。這個問題,下文會詳細分析。參數的話,很簡單,就是終端的命令即可。這是因為system函數的實現中調用了shell的緣故。 system優缺點 優點: 可以讓c ...
  • 環境:ubuntu16.04 交叉編譯器版本:4.8.3 依賴x264,lame x264: 1.wget ftp://ftp.videolan.org/pub/x264/snapshots/last_stable_x264.tar.bz2 2.tar xvf last_stable_x264.ta ...
  • (green) short pairwise alignment / detailed edit model; (yellow) database search / divergent homology detection; (red) whole genome alignment / alignm ...
  • 參數檢查 跨目錄執行腳本但維持腳本與"."的相對位置不變 彩色日誌輸出 使用臨時文件, 防止使用的文件與已知文件重名而被替換 輸出到標準輸出的同時寫入到文件 逐行處理文本文件,註意文本要set fileformat=unix確保格式正確 ...
  • 添加引用 創建表 創建列 創建行 賦值和取值 篩選行 刪除行 複製表 表排序 ...
  • 前段時間,用CefSharp.WinForms寫了一個可以播放flash以及一些展示頁面的小程式,涉及到跨域訪問之類的問題。CefSharp.WinForms版本49.0.1。 剛開始挺順利,做到播放flash的時候各種黑屏,無法播放。先是回退32那個版本 用NPAPI解決的但是貌似32那個版本在客 ...
  • 在《asp.net core認證與授權》中講解了固定和自定義角色授權系統許可權,其實我們還可以通過其他方式來授權,比如可以通過角色組,用戶名,生日等,但這些主要取決於ClaimTypes,其實我們也可以自定義鍵值來授權,這些統一叫策略授權,其中更強大的是,我們可以自定義授權Handler來達到靈活授權... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...