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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET WebAPI 集成 Swagger 启用 OAuth 2.0 配置问题

發布時間:2025/3/15 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET WebAPI 集成 Swagger 启用 OAuth 2.0 配置问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在 ASP.NET WebAPI 集成 Swagger 后,由于接口使用了 IdentityServer 做的認證,調試起來很不方便;看了下 Swashbuckle 的文檔 ,是支持 OAuth2.0 的配置的,使用的簡化模式(Implicit grant type),交互的流程如下:

Implicit Grant Type (簡化模式)

參數:

  • response_type:表示授權類型,此處的值固定為"token",必選項。
  • client_id:表示客戶端的ID,必選項。
  • redirect_uri:表示重定向的URI,可選項。
  • scope:表示權限范圍,可選項。
  • state:表示客戶端的當前狀態,可以指定任意值,認證服務器會原封不動地返回這個值。
GET /authorize?response_type=token&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1Host: server.example.com

認證服務器回應客戶端的URI,包含以下參數:

  • access_token:表示訪問令牌,必選項。
  • token_type:表示令牌類型,該值大小寫不敏感,必選項。
  • expires_in:表示過期時間,單位為秒。如果省略該參數,必須其他方式設置過期時間。
  • scope:表示權限范圍,如果與客戶端申請的范圍一致,此項可省略。
  • state:如果客戶端的請求中包含這個參數,認證服務器的回應也必須一模一樣包含這個參數。

???? HTTP/1.1 302 Found
???? Location:
http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA
?????????????? &state=xyz&token_type=example&expires_in=3600

Swagger 啟用 OAuth 2.0 配置

Idrv 中配置客戶端(Client)

new Client{ClientName = "Test_API_Flow",ClientId = "api_test_api_flow",Flow = Flows.Implicit,ClientUri = "https://identityserver.io",RequireConsent = true,AllowRememberConsent = true,RedirectUris = new List<string>{"http://localhost:39106/swagger/ui/o2c-html",},AllowedCorsOrigins = new List<string>{"http://localhost:39106"},AccessTokenLifetime = 3600,AccessTokenType = AccessTokenType.Jwt,AllowAccessToAllScopes=true},

API:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions{Authority = IdsvSetting.Authority,ValidationMode = ValidationMode.ValidationEndpoint,RequiredScopes=new List<string> {"all","user","order"}} }); /// <summary>/// 早餐控制器/// </summary>[RoutePrefix("api/v1/breakfast")]public class BreakfastController : ApiController{private static readonly Logger logger = LogManager.GetCurrentClassLogger();/// <summary>/// 早餐服務/// </summary>private readonly IBreakfastService _breakfastService;/// <summary>/// 構造方法/// </summary>/// <param name="breakfastService">早餐服務</param>public BreakfastController(IBreakfastService breakfastService){_breakfastService = breakfastService;}#region 獲得酒店關聯的餐廳的酒店/// <summary>/// 獲得酒店關聯的餐廳的酒店/// </summary>/// <param name="hotelcd">酒店編號</param>/// <returns>獲得酒店關聯的餐廳的酒店</returns> [Authorize][HttpGet][Route("{hotelcd}/mapping")]public async Task<IHttpActionResult> GetXhotelBreakfastHotelMappingRequest(string hotelcd){var response = await _breakfastService.GetXhotelBreakfastHotelMappingRequest(hotelcd);return Json(response);}#endregion} }

配置 SwaggerConfig

//https://tsso.xxx.cn/connect/authorize?response_type=token&redirect_uri=http%3A%2F%2Flocalhost%3A39106%2Fswagger%2Fui%2Fo2c-html&realm=test-realm&client_id=api_test_api_flow&scope=all%20%20&state=oauth2c.OAuth2("oauth2").Description("OAuth2 Implicit Grant").Flow("implicit").AuthorizationUrl("https://tsso.xxx.cn/connect/authorize")//.TokenUrl("https://sso.xxx.cn/connect/token").Scopes(scopes =>{scopes.Add("all", "all access to protected resources");scopes.Add("user", "user access to protected resources");scopes.Add("order", "order access to protected resources");}); ...??

c.OperationFilter<AssignOAuth2SecurityRequirements>();c.EnableOAuth2Support(clientId:
"api_test_api_flow",clientSecret: null,realm: "test-realm",appName: "Swagger UI"//additionalQueryStringParams: new Dictionary<string, string>() { { "foo", "bar" } }); public class AssignOAuth2SecurityRequirements : IOperationFilter{public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription){var actFilters = apiDescription.ActionDescriptor.GetFilterPipeline();var allowsAnonymous = actFilters.Select(f => f.Instance).OfType<OverrideAuthorizationAttribute>().Any();if (allowsAnonymous)return; // must be an anonymous method//var scopes = apiDescription.ActionDescriptor.GetFilterPipeline()// .Select(filterInfo => filterInfo.Instance)// .OfType<AllowAnonymousAttribute>()// .SelectMany(attr => attr.Roles.Split(','))// .Distinct();if (operation.security == null)operation.security = new List<IDictionary<string, IEnumerable<string>>>();var oAuthRequirements = new Dictionary<string, IEnumerable<string>>{{"oauth2", new List<string> {"all","user","order"}}};operation.security.Add(oAuthRequirements);}}

OK ,配置完成,點擊紅色的圈圈,登錄成功會302到? http://localhost:39106/swagger/ui/o2c-htm 上

當然也可以退出授權:

REFER:

https://www.scottbrady91.com/Identity-Server/ASPNET-Core-Swagger-UI-Authorization-using-IdentityServer4
https://stackoverflow.com/questions/33752900/enable-oauth2-client-credentials-flow-in-swashbuckle
https://stackoverflow.com/questions/29275499/swagger-swashbuckle-oauth2-with-resource-owner-password-credentials-grant?rq=1
http://knowyourtoolset.com/2015/08/secure-web-apis-with-swagger-swashbuckle-and-oauth2-part-2/

轉載于:https://www.cnblogs.com/Irving/p/7275065.html

總結

以上是生活随笔為你收集整理的ASP.NET WebAPI 集成 Swagger 启用 OAuth 2.0 配置问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日b视频免费观看 | 韩国精品一区 | 精品国产一区二区三区久久久久久 | a级在线看| 亚洲精品乱码久久久久久久久久久久 | 久久久久亚洲 | 伊人网站| 国产性―交一乱―色―情人 | 国产免费激情 | 99久久久无码国产精品性波多 | a级国产毛片 | 久久91精品国产91久久小草 | 无码人妻精品一区二区蜜桃色欲 | 久久久久国产免费 | 亚洲精品人 | 日批在线播放 | 激情久久免费视频 | 性欧美69| 丝袜制服中文字幕 | 日韩成人在线一区 | 毛片大全在线观看 | caoporn国产| 国产白浆一区二区 | 靠逼视频网站 | av在线天堂网| 久久精品天天中文字幕人妻 | 国产传媒在线 | 日韩高清黄色 | 日韩在线精品视频 | 在线观看免费国产 | 欧美黄色免费观看 | 97caocao| 欧美男女激情 | 色翁荡息又大又硬又粗又爽 | 国产成人精品一区二区三区网站观看 | 黄色成人在线 | 青青青国产在线 | 国模大胆一区二区三区 | 亚洲一本在线观看 | 一级特级片 | 99久久精品日本一区二区免费 | 超碰人人cao | 亚洲国产精品影院 | 红猫大本营在线观看的 | 久草视频免费在线观看 | www.97色| 午夜在线视频观看 | 欧美专区 日韩专区 | 精品无码久久久久久久久 | 青青国产在线观看 | 欧美激情一区二区三区四区 | 国产 日韩 欧美 制服丝袜 | 最新免费av网站 | 午夜xxx| 女女调教被c哭捆绑喷水百合 | 艳妇臀荡乳欲伦交换电影 | av站 | 国产精品宾馆在线精品酒店 | 一级作爱片 | av免费在线网站 | 老司机深夜影院 | 午夜黄色一级片 | 色呦呦一区二区 | 国产欧美一区二区三区白浆喷水 | 日本不卡一区二区三区视频 | 激情久久久久久久 | 一区二区三区视频网 | 日韩av色 | 国产夫妻在线观看 | 欧美黑吊大战白妞 | 精品人妻少妇AV无码专区 | 特大黑人娇小亚洲女mp4 | 男人的天堂狠狠干 | www.桃色av嫩草.com | 蜜桃av成人永久免费 | 中文字幕高清在线播放 | 日本ww色 | 一本大道综合伊人精品热热 | 天天操天天爱天天干 | 日韩精品极品视频 | 国产视频xxx | 粉色视频网站 | 国产精品美女网站 | 国产婷婷色一区二区三区 | 黄色一级免费看 | 欧美激情精品久久久久久蜜臀 | 奇米影视播放器 | 欧美a级在线免费观看 | 国产一区二区三区麻豆 | 久草五月 | 日韩中文字幕2019 | 国产成人精品一区二区三 | 一级草逼片 | 欧美综合亚洲图片综合区 | 欧产日产国产精品98 | 91网站免费| 亚洲一区高清 | 你懂的在线网站 | 日韩欧美国产中文字幕 |