【发布时间】:2019-03-20 04:47:20
【问题描述】:
在 Owin 中间件启动类中,我添加了 OIDC 身份验证,其中响应类型为“code id_token”。有了这个中间件,我可以访问我的授权控制器。但问题是,我无法使用此中间件访问同一域中的 API。
我正在使用我存储在 userClaim 中的 access_token。但它正在返回 IdentityServer4 登录页面的 HTML。
[Filters.AuthorizeOIDC(Roles = "dukkan.sa")]
public async Task<ActionResult> ViewApiResult()
{
var user = User as System.Security.Claims.ClaimsPrincipal;
var token = user.FindFirst("access_token").Value;
var result = await CallApi(token);
ViewBag.Json = result;
return View();
}
private async Task<string> CallApi(string token)
{
var client = new HttpClient();
client.SetBearerToken(token);
var json = await client.GetStringAsync("http://localhost:57346/api/SampleApi");
return json;
}
我获得的保护 MVC API 的示例是使用 IdentityServer3。他们在 API 访问请求期间使用 IdentityServer3.AccessTokenValidation 包从后台通道对客户端进行身份验证:
app.UseOAuthBearerAuthentication(new IdentityServerBearerTokenAuthenticationOptions { Authority = "https://localhost:44319/identity", RequiredScopes = new[] { "sampleApi" } });
但 IdentityServer4.AccessTokenValidation 不适用于 MVC5。我可以在 MVC 5 中使用 IdentityServer3.AccessTokenValidation。但这是接受低于 2.0.0 版本的 IdentityModel。
需要解决方案。 IdentityServer4 没有正确支持 MVC。
【问题讨论】:
-
曾经成功过吗?
-
嗨,@RuardvanElburg,目前我已将我的 MVC webAPI2 转换为 .net Core 2 以使用 IdentityServer4 对其进行身份验证。稍后,我将通过在 .net 4.5 中制作我的完整项目而不是当前的 .net v4.6.2 来再次尝试。
标签: authentication asp.net-mvc-5 access-token identityserver4 openid-connect