Ran*_*nan 3 asp.net asp.net-core identityserver4
我对中间件概念比较陌生.我知道中间件在完成时会调用下一个中间件.
我正在尝试使用Google或我的身份服务器验证请求.用户可以使用谷歌或本地帐户登录我的移动应用程序.但是,我无法弄清楚如何使用这两种身份验证中间件.如果我为谷歌传递id_token,它会传递第一个中间件(UseJwtBearerAuthentication),但在第二个中间件()上失败UseIdentityServerAuthentication.我怎样才能使它在实际传递至少1个身份验证中间件时不会抛出错误?例如,如果它传递第一个中间件,第二个中间件会被忽略?
app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
Authority = "https://accounts.google.com",
Audience = "secret.apps.googleusercontent.com",
TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = true,
ValidIssuer = "accounts.google.com"
},
RequireHttpsMetadata = false
});
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:1000/",
RequireHttpsMetadata = false,
ScopeName = "MyApp.Api"
});
Run Code Online (Sandbox Code Playgroud)
通常,当身份验证中间件失败时(我并不意味着抛出异常),这不会影响另一个成功的身份验证中间件.可能你的第二个中间件抛出异常(不是验证失败).首先检查错误消息并尝试解决它.如果不能,请使用AuthenticationFailed事件来处理错误.在这种情况下,您的代码应如下所示:
app.UseJwtBearerAuthentication(new JwtBearerOptions()
{
// ...
Events = new JwtBearerEvents()
{
OnAuthenticationFailed = async (context) =>
{
if (context.Exception is your exception)
{
context.SkipToNextMiddleware();
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是,对于你的场景,我不会选择你的方式.我只使用身份服务器端点.要使用Google签名,您可以配置身份服务器,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = false,
AutomaticChallenge = false
});
app.UseGoogleAuthentication(new GoogleOptions
{
AuthenticationScheme = "Google",
SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
ClientId = "",
ClientSecret = ""
});
app.UseIdentityServer();
Run Code Online (Sandbox Code Playgroud)
编辑
似乎AuthenticationFailed事件不能用于IdentityServer4.AccessTokenValidation.我不确定,但如果您将身份服务器仅用于jwt令牌,则可以UseJwtBearerAuthentication用于验证.
| 归档时间: |
|
| 查看次数: |
4663 次 |
| 最近记录: |