我們有一個使用OpenIdDict進行身份驗證的asp.net核心Web應用程序。我注意到,我未經身份驗證的Ajax調用返回了200以及我們的登錄表單。從我讀過的這是預期的行爲,因爲OpenIdDict處理請求,然後ASP.NET核心處理它並返回200. ASP.NET核心處理它,因爲在Startup.cs中調用'UseIdentity()'。我所見過的所有OpenIdDict實例都調用了'UseIdentity'。我有2個問題。使用openIdDict我們必須在Startup.cs中使用'UseIdentity()'嗎?
- 如果我不想ASP.NET核心來處理我的請求,我可以只刪除「UseIdentity()」?我試過了,現在我得到了401而不是200.這樣做有沒有什麼壞處,或者OpenIdDict是否需要'UseIdentity()'?
- 如果我不想失去重定向登錄UI視圖的能力是完成此操作以覆蓋OnRedirectToLogin的最好/最簡單/最安全的方法?下面的代碼示例:
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents { OnRedirectToLogin = ctx => { if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == (int) HttpStatusCode.OK) { ctx.Response.StatusCode = (int) HttpStatusCode.Unauthorized; } else { ctx.Response.Redirect(ctx.RedirectUri); } return Task.FromResult(0); } };
代碼示例源:https://devblog.dymel.pl/2016/07/07/return-401-unauthorized-from-asp-net-core-api/
這個問題在這裏的進一步討論:https://github.com/aspnet/Security/issues/804
這很好。謝謝! – Bloodhound
您是否有示例項目或指南,可以使用Core 2.0和OpenIdDict 2.x做同樣的事情? – Bloodhound
所有OpenIddict示例已更新爲2.x:https:// github。COM/openiddict/openiddict樣本。遺憾的是,2.x中不再支持基於路徑的身份驗證中間件選擇,並且推薦使用'[Authorize(AuthenticationSchemes =「Bearer」)]方法。閱讀https://github.com/aspnet/Announcements/issues/262瞭解更多信息。 – Pinpoint