我有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