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

歡迎訪問 默认站点!

默认站点

當前位置: 首頁 >

asp.net core AuthenticationMiddleware 在WebApi中的的使用

發布時間:2023/12/4 24 豆豆
默认站点 收集整理的這篇文章主要介紹了 asp.net core AuthenticationMiddleware 在WebApi中的的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在.net framework 4.5架構下使用認證(Authentication)授權(Authorization)。

IIS使用HttpModule進行認證(Authentication),我們可以選擇自己實現認證方式并在web.config中配置,當然也可以選擇IIS默認提供的幾種實現,這里不再繼續展開討論。?

asp.net core默認提供了幾種默認的實現方式,包括Identity,Facebook, Google, Microsoft Account, Twitter 等等。這里介紹Basic Authentication認證方式。asp.net core的請求通道由一系列的請求委托組成,一個一個順序執行。

實現Basic Authentication最簡單的方式是添加一個中間件。新建文件BasicAuthenticationMiddlerware

public sealed class BasicAuthenticationMiddlerware

? ? {

? ? ? ? private readonly RequestDelegate _next;


? ? ? ? public BasicAuthenticationMiddlerware(RequestDelegate next)

? ? ? ? {

? ? ? ? ? ? _next = next;

? ? ? ? }


? ? ? ? public async Task InvokeAsync(HttpContext context)

? ? ? ? {

? ? ? ? ? ? string authentication = context.Request.Headers["Authorization"];

? ? ? ? ? ? if (authentication != null && authentication.Contains("Basic"))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //Extract credentials

? ? ? ? ? ? ? ? var usernamePasswordStr = authentication.Trim().Split(" ")[1];


? ? ? ? ? ? ? ? var userNamAndPasswordArr = usernamePasswordStr.Split(':');

? ? ? ? ? ? ? ? if (userNamAndPasswordArr.Length != 2)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? context.Response.StatusCode = 401;

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? var username = userNamAndPasswordArr[0];

? ? ? ? ? ? ? ? var password = userNamAndPasswordArr[1];


? ? ? ? ? ? ? ? /*

? ? ? ? ? ? ? ? ?* 根據用戶賬號密碼驗證用戶有效性

? ? ? ? ? ? ? ? ?* 如果有效

? ? ? ? ? ? ? ? ?* 執行 await _next.Invoke(context);

? ? ? ? ? ? ? ? ?* 否則

? ? ? ? ? ? ? ? ?* context.Response.StatusCode = 401;

? ? ? ? ? ? ? ? ?*/


? ? ? ? ? ? ? ? if (true)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? await _next.Invoke(context);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? context.Response.StatusCode = 401;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? context.Response.StatusCode = 401;?

? ? ? ? ? ? }

? ? ? ? ? ??

? ? ? ? }

? ? }

完成中間件的定義以后,在Startup.cs文件的Configure方法中注冊中間件以開啟驗證。注意,這里一定要添加在app.UseMvc()之前。

app.UseMiddleware<BasicAuthenticationMiddlerware>();?

或者通過添加IApplicationBuilder的擴張方法,再用擴展方法進行注冊。代碼如下

public static class BasicAuthenticationMiddlerwareExtension

? ? {

? ? ? ? public static IApplicationBuilder UseBasicAuthenticationMiddlerware(

? ? ? ? ? ? this IApplicationBuilder builder)

? ? ? ? {

? ? ? ? ? ? return builder.UseMiddleware<BasicAuthenticationMiddlerware>();

? ? ? ? }

? ? }

Startup.cs的Configure的內容如下


public void Configure(IApplicationBuilder app, IHostingEnvironment env)?

{

? ? ?if (env.IsDevelopment())

? ? ?{

? ? ? ? app.UseDeveloperExceptionPage();

? ? ?}

? ? ? ?app.UseBasicAuthenticationMiddlerware();

? ? ? ?app.UseMvc();

}


啟動WebApi。不添加頭文件Authorization,如預期返回401狀態碼。

?

添加頭部信息,如預期返回數據。

?

原文地址:http://www.cnblogs.com/Zhang-Xiang/p/7536803.html


.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

總結

以上是默认站点為你收集整理的asp.net core AuthenticationMiddleware 在WebApi中的的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網站內容還不錯,歡迎將默认站点推薦給好友。