[英]Authentication using Auth headers like webAPI but in MVC
好吧,讀這篇文章的人可能都知道(我也是如此)WebAPI如何工作以及如何使用WebAPI和Identity框架構建應用程序我可以構建一個http請求,添加一個auth標頭,應用程序將通過閱讀auth知道我是誰頭。
這就是所謂的“無狀態”API調用,其中接收調用的API的所有內容都被賦予了確定用戶身份所需的所有內容,因此可以對用戶進行身份驗證並對其請求進行“無狀態”操作。
.....
我想在MVC(而不是Web API)中使用這種完全相同的行為。
我希望以前沒有對此應用程序提出任何請求,我的應用程序使用Identity框架的方式與我的WebAPI端點完全相同,以確保使用Authorization標頭對每個“無狀態”調用進行身份驗證。
這是我在WebAPI中做的方式(在MVC中不起作用)......
using Core.App.Security;
using Microsoft.Owin.Security.OAuth;
using Ninject;
using Owin;
namespace Core.App
{
/// <summary>
/// Setup as per ...
/// Source code: https://github.com/tjoudeh/AngularJSAuthentication
/// walkthrough: http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/
/// </summary>
public static class Auth
{
public static void Configure(IAppBuilder app, IKernel kernel)
{
// ensure that owin creates the required UserManager & sign in manager per owin instance
app.CreatePerOwinContext<ApplicationUserManager>((options, owinContext) => ApplicationUserManager.Create(options, owinContext, kernel));
app.CreatePerOwinContext<ApplicationSignInManager>((options, owinContext) => ApplicationSignInManager.Create(options, owinContext, kernel));
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
app.AddAuthInfoToContext();
}
}
}
本質上,這使用令牌設置承載auth,但不是當前應用程序中的auth服務器。
這段代碼允許我作為我的Auth令牌提供者的集中式SSO服務器可以發出我可以對任何API應用程序使用的令牌(是的,我有很多)。
差異
WebAPI將在當前標識(HttpContext.Current.Identity)中具有授權用戶信息,並且不依賴於會話/先前的登錄調用來創建會話信息。
MVC依賴會話和用戶采用一種更傳統的“Forms Auth”類型方法,這意味着我必須在我關心的請求之前向本地MVC應用程序發出auth請求以“登錄”。
所以問題是
當這是該用戶在該會話中發出的第一個請求時,如何在使用上述代碼與Identity框架時使用WebAPI時,如何在典型的http get請求中使用授權標頭令牌“登錄”?
使用auth標頭中的令牌信息來檢索用戶並在管道的早期階段將其放入當前標識(可能在auth階段)
當我使用owin來設置我的應用程序時,我可以輕松定義一個自定義中間件,以根據令牌信息將所需的更改應用於當前身份。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.