我有3層應用程序結構。有一個適用於最終用戶的cordova js應用程序,一個用作OpenID權限的identityserver3的實現,以及一個將通過cordova應用程序中的應用程序內瀏覽器訪問的MVC應用程序。如何將不記名令牌轉換爲MVC應用程序的驗證Cookie
用戶的入口是cordova應用程序。他們通過應用內瀏覽器登錄,然後訪問應用程序功能或點擊鏈接打開應用程序內瀏覽器並訪問MVC應用程序。
我們對固定MVC網站的策略是利用承載令牌認證,因爲我們已經從應用程序登錄一次,不想再提示用戶登錄的時候被定向到MVC應用程序:
app.Map("/account", account =>
{
account.UseIdentityServerBearerTokenAuthentication(new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions()
{
Authority = "https://localhost:44333/core",
RequiredScopes = new string[] { "scope" },
DelayLoadMetadata = true,
TokenProvider = new QueryStringOAuthBearerProvider(),
ValidationMode = ValidationMode.ValidationEndpoint,
});
}
由於持續的查詢字符串的access_token是痛苦的,我實現了自定義OAuthBearerAuthenticationProvider:
public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider
{
private static ILog logger = LogManager.GetLogger(typeof(QueryStringOAuthBearerProvider));
public override Task RequestToken(OAuthRequestTokenContext context)
{
logger.Debug($"Searching for query-string bearer token for authorization on request {context.Request.Path}");
string value = GetAccessTokenFromQueryString(context.Request);
if (!string.IsNullOrEmpty(value))
{
context.Token = value;
//Save the token as a cookie so the URLs doesn't need to continue passing the access_token
SaveAccessTokenToCookie(context.Request, context.Response, value);
}
else
{
//Check for the cookie
value = GetAccessTokenFromCookie(context.Request);
if (!string.IsNullOrEmpty(value))
{
context.Token = value;
}
}
return Task.FromResult<object>(null);
}
[cookie access methods not very interesting]
}
這工作,並允許MVC應用程序不必堅持訪問令牌到每一個請求,但小號將訪問令牌作爲一個通用的cookie看起來是錯誤的。
我真正想做的是使用訪問令牌來處理OpenID端點,併發出一個form-auth樣式的cookie,它會響應註銷。我發現我可以添加account.UseOpenIdConnectAuthentication(..)
,但是如果我通過access_token進行身份驗證,則只需跳過OpenIdConnectAuthentication位。有任何想法嗎?
有沒有一個鉤子的地方,我可以注入該邏輯? BearerTokenAuth位沒有任何回調,並且「UseOpenIdConnectAuthentication」中提供的通知似乎不合適。也許認證過濾器屬性? – K0D4
UseOpenIdConnectAuthentication MW爲您發佈cookie。如果您需要更改Cookie中發佈的聲明,請處理SecurityTokenValidated事件。 –
感謝Brock的澄清,如果我理解正確,我應該完全刪除承載身份驗證配置並將其替換爲UseOpenIDConnectAuthentication。在那裏,我將查看查詢字符串,取出令牌,然後使用OIDC進行授權,處理ClaimsIdentity並設置身份驗證票證。另外,如果我同時保留兩種身份驗證方法,我需要有一種方法來鏈接它們,但是當我有一個BearerToken時,另一個身份驗證例程完全被忽略。 – K0D4