如何访问使用IdentityServer4授权的MVC API

问题描述 投票:0回答:1

在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。他们使用IdentityServer3.AccessTokenValidation包在API访问请求期间从后台通道验证客户端:

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。

authentication asp.net-mvc-5 access-token identityserver4 oidc
1个回答
0
投票

为什么要将IdentityServer4.AccessTokenValidation与MVC5一起使用?因为服务器是IdentityServer4?

没有必要这样做。 IdentityServer3和IdentityServer4构建在相同的OpenId Connect规范之上,这意味着您可以在服务器是IdentityServer4时为客户端使用IdentityServer3.AccessTokenValidation。

实际上,您可以使用根据OpenId Connect规范构建的客户端上的任何代码。我建议你试试IdentityServer3.AccessTokenValidation。

© www.soinside.com 2019 - 2024. All rights reserved.