前言 很多時候其實我們並不需要asp.net core自帶的那麼複雜的用戶系統,基於角色,各種概念,還得用EF Core,而且在web應用中都是把信息存儲到cookie中進行通訊(我不喜歡放cookie中,因為有次我在mac系統中的safari瀏覽器運行web應用時,碰到跨域cookie設不上,非要 ...
前言
很多時候其實我們並不需要asp.net core自帶的那麼複雜的用戶系統,基於角色,各種概念,還得用EF Core,而且在web應用中都是把信息存儲到cookie中進行通訊(我不喜歡放cookie中,因為有次我在mac系統中的safari瀏覽器運行web應用時,碰到跨域cookie設不上,非要使用個很特殊的方法,記得是iframe,挺麻煩的,所以我還是喜歡放自定義header中), 用了以後感覺被微軟給綁架了。不過這完全是個人喜好,大家完全可以按自己喜歡的來,我這裡提供了另外一條路,大家可以多一種選擇。
我這邊是利用asp.net core的依賴註入,定義了一套屬於自己系統的用戶認證與授權,大家可以參考我這個來定義自己的,也不局限於用戶系統。
面向切麵編程(AOP)
在我看來,Middleware與Filter都是asp.net core中的切麵,我們可以把認證與授權放到這兩塊地方。我個人比較喜歡把認證放到Middleware,可以提早把那些不合法的攻擊攔截返回。
依賴註入(DI)
依賴註入有3種生命周期
1. 在同一個請求發起到結束。(services.AddScoped)
2. 每次註入的時候都是新建。(services.AddTransient)
3. 單例,應用開始到應用結束。(services.AddSingleton)
我的自定義用戶類採用的是services.AddScoped。
具體做法
1. 定義用戶類
1 // 用戶類,隨便寫的 2 public class MyUser 3 { 4 public string Token { get; set; } 5 public string UserName { get; set; } 6 }
2. 註冊用戶類
Startup.cs中的ConfigureServices函數:
1 // This method gets called by the runtime. Use this method to add services to the container. 2 public void ConfigureServices(IServiceCollection services) 3 { 4 ... 5 // 註冊自定義用戶類 6 services.AddScoped(typeof(MyUser)); 7 ... 8 }
自定義用戶類,是通過services.AddScoped方式進行註冊的,因為我希望它在同一個請求中,Middleware, filter, controller引用到的是同一個對象。
3. 註入到Middleware
1 // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project 2 public class AuthenticationMiddleware 3 { 4 private readonly RequestDelegate _next; 5 private IOptions<HeaderConfig> _optionsAccessor; 6 7 public AuthenticationMiddleware(RequestDelegate next, IOptions<HeaderConfig> optionsAccessor) 8 { 9 _next = next; 10 _optionsAccessor = optionsAccessor; 11 } 12 13 public async Task Invoke(HttpContext httpContext, MyUser user) 14 { 15 var token = httpContext.Request.Headers[_optionsAccessor.Value.AuthHeader].FirstOrDefault(); 16 if (!IsValidate(token)) 17 { 18 httpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; 19 httpContext.Response.ContentType = "text/plain"; 20 await httpContext.Response.WriteAsync("UnAuthentication"); 21 } 22 else 23 { 24 // 設置用戶的token 25 user.Token = token; 26 await _next(httpContext); 27 } 28 } 29 30 // 隨便寫的,大家可以加入些加密,解密的來判斷合法性,大家自由發揮 31 private bool IsValidate(string token) 32 { 33 return !string.IsNullOrEmpty(token); 34 } 35 } 36 37 // Extension method used to add the middleware to the HTTP request pipeline. 38 public static class AuthenticationMiddlewareExtensions 39 { 40 public static IApplicationBuilder UseAuthenticationMiddleware(this IApplicationBuilder builder) 41 { 42 return builder.UseMiddleware<AuthenticationMiddleware>(); 43 } 44 }
我發現如果要把介面/類以Scoped方式註入到Middleware中,就需要把要註入的類/介面放到Invoke函數的參數中,而不是Middleware的構造函數中,我猜這也是為什麼Middleware沒有繼承基類或者介面,在基類或者介面中定義好Invoke的原因,如果它在基類或者介面中定義好Invoke,勢必這個Invoke的參數要固定死,就不好依賴註入了。
4. 配置某些路徑才會使用該Middleware
1 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 2 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 3 { 4 loggerFactory.AddConsole(Configuration.GetSection("Logging")); 5 loggerFactory.AddDebug(); 6 // Set up nlog 7 loggerFactory.AddNLog(); 8 app.AddNLogWeb(); 9 10 // 除了特殊路徑外,都需要加上認證的Middleware 11 app.MapWhen(context => !context.Request.Path.StartsWithSegments("/api/token") 12 && !context.Request.Path.StartsWithSegments("/swagger"), x => 13 { 14 // 使用自定義的Middleware 15 x.UseAuthenticationMiddleware(); 16 // 使用通用的Middleware 17 ConfigCommonMiddleware(x); 18 }); 19 // 使用通用的Middleware 20 ConfigCommonMiddleware(app); 21 22 // Enable middleware to serve generated Swagger as a JSON endpoint. 23 app.UseSwagger(); 24 25 // Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint. 26 app.UseSwaggerUI(c => 27 { 28 c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); 29 }); 30 } 31 32 // 配置通用的Middleware 33 private void ConfigCommonMiddleware(IApplicationBuilder app) 34 { 35 // cors 36 app.UseCors("AllowAll"); 37 38 app.UseExceptionMiddleware(); 39 // app.UseLogRequestMiddleware(); 40 app.UseMvc(); 41 }
像獲取token啊,查看api文檔啊就不需要認證了。
5. 註入到Filter
1 public class NeedAuthAttribute : ActionFilterAttribute 2 { 3 private string _name = string.Empty; 4 private MyUser _user; 5 6 public NeedAuthAttribute(MyUser user, string name = "") 7 { 8 _name = name; 9 _user = user; 10 } 11 12 public override void OnActionExecuting(ActionExecutingContext context) 13 { 14 this._user.UserName = "aaa"; 15 } 16 }
這裡我創建的是個帶字元串參數的類,因為考慮到這個Filter有可能會被覆用,比如限制某個介面只能被某種用戶訪問, 這個字元串便可以存某種用戶的標識。
Filter中還可以註入資料庫訪問的類,這樣我們便可以到資料庫中通過token來獲取到相應的用戶信息。
6. 使用Filter
1 [TypeFilter(typeof(NeedAuthAttribute), Arguments = new object[]{ "bbb" }, Order = 1)] 2 public class ValuesController : Controller
這裡使用了TypeFilter,以載入使用了依賴註入的Filter, 並可以設置參數,跟Filter的順序。
預設Filter的順序是 全局設置->Controller->Action, Order預設都為0,我們可以通過設置Order來改變這個順序。
7. 註入到Controller
1 public class ValuesController : Controller 2 { 3 private MyUser _user; 4 5 public ValuesController(MyUser user) 6 { 7 _user = user; 8 } 9 ... 10 }
註入到Controller的構造函數中,這樣我們就可以在Controller的Action中使用我們自定義的用戶,就能知道到底當前是哪個用戶在調用這個Action。