我有一个三层的应用程序结构。有一个针对最终用户的cordova js应用程序,一个充当OpenID授权机构的identityserver3的实现以及一个可以通过cordova应用程序中的应用程序内浏览器访问的MVC应用程序。
用户的起始入口是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端点一起使用,并发出一个Forms-Auth样式cookie,该cookie响应注销。我发现可以添加,account.UseOpenIdConnectAuthentication(..)
但是如果我通过access_token进行身份验证,则会跳过OpenIdConnectAuthentication位。有任何想法吗?
您不需要-访问令牌旨在用于调用Web API。您可以使用OIDC的id_token对用户进行身份验证,并根据内部声明发出本地身份验证cookie。Microsoft OpenIdConnect身份验证中间件将为您完成大部分繁重的工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句