[英]Replacing Cookie by Token based authentication in ASP.NET OWIN OpenIdConnect code authorization flow
我們有一個用ASP.NET編寫的Web應用程序,它使用MVC為我們的單頁應用程序和用於ajax調用的Web API提供服務。
身份驗證使用Microsoft.Owin和OpenIdConnect與Azure AD for Authority。 OAUTH流程是服務器端代碼授權。 然后在Startup.Auth.cs中我們有
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
var cookieAuthenticationOptions = new CookieAuthenticationOptions()
{
CookieName = CookieName,
ExpireTimeSpan = TimeSpan.FromDays(30),
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
SlidingExpiration = true,
};
app.UseCookieAuthentication(cookieAuthenticationOptions);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
AuthorizationCodeReceived = (context) =>
{
/*exchange authorization code for a token
stored on database to access API registered on AzureAD (using ADAL.NET) */
},
RedirectToIdentityProvider = (RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) =>
{
/* Set the redirects URI here*/
},
});
}
單擊登錄時,我們導航到一個URL,其路由映射到以下MVC控制器的方法
public class AccountController : Controller
{
public void SignIn(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext()
.Authentication.Challenge(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
public void SignOut(string signalrRef)
{
var authenticationProperties = /* Proper auth properties, redirect etc.*/
HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties,
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
}
然后,連接到我們的應用程序的最終用戶使用ASP.NET cookie在我們的客戶端應用程序和ASP.net服務器之間進行身份驗證。 我們想要使用基於令牌的方法 。 如果您有興趣, 這就是原因 。
我試圖用Microsoft.Owin.Security.OAuth替換Nuget包Microsoft.Owin.Security.Cookies並在Startup.cs中替換
app.UseCookieAuthentication(cookieAuthenticationOptions);
by app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
在我的AccountController中,我們改變了來自HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
的挑戰HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
to HttpContext.GetOwinContext().Authentication.SignOut(authenticationProperties, OpenIdConnectAuthenticationDefaults.AuthenticationType, OAuthDefaults.AuthenticationType);
問題是,使用Cookie時,當流程完成時, set-cookie會在Web請求中自動發送,同時重定向到我們指定的url。 我在哪里可以找到OWIN使用UseOAuthBearerAuthentication生成的承載(如果有的話)**,**我應該何時何地將其發送回我的客戶端SPA
注意:可以在此github存儲庫中找到我們嘗試執行的操作的開源示例。
我認為有兩種方法可供您考慮。
CookieAuthenticationMiddleware
的使用(盡管我不是100%肯定)。 您可以在提及的AuthorizationCodeReceived
通知中捕獲令牌,並且可以使用URL片段中的令牌重定向回SPA。 您還可以使用一些路由將令牌(緩存在您的服務器上)下載到您的javascript。 在任何一種情況下,您都需要確保外部呼叫者無法訪問您的令牌。 需要記住的是如何在令牌過期時刷新令牌。 如果您使用#1,大部分將由圖書館為您處理。 如果你使用#2,你將不得不自己管理它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.