【问题标题】:Identity Server 4 User management APIIdentity Server 4 用户管理 API
【发布时间】:2018-02-02 14:06:21
【问题描述】:

我设法获得了一个与单页应用程序项目 (ReactJS)、在 ASP.net Core 项目和 IdentityServer 4 项目上运行的 API 一起使用的解决方案。

我希望能够从单页应用程序项目中调用 IdentityServer 4 项目的 API。

我在IdentityServer 4 应用程序中创建了一个简单的控制器类,带有授权属性。但是,如果我通过Postman 调用它,我会返回登录页面的 HTML。

这发生在我已经登录 API 并且我使用相同的令牌之后。

我应该如何登录身份服务器来调用它来管理用户?

【问题讨论】:

标签: asp.net-core identityserver4 asp.net-core-webapi


【解决方案1】:

如 cmets 中所述,您绝对应该为您的问题添加更多信息。您的控制器是身份服务器的 mvc 应用程序的一部分吗?你在使用 AspnetCore.Identity 吗?

如果是这样,您的控制器受 AspnetCore.Identities 的 cookie 身份验证方案的保护。您需要发送 cookie 才能访问控制器。这与身份服务器无关,因为你在本地 MVC 应用程序上,它只是普通的 MVC。

邮递员在发送 cookie 时遇到问题,您需要 interceptor chrome extension。您还需要通过邮递员登录。

如果 SPA 由同一个 MVC 应用程序托管,这可能会起作用。如果没有,您将需要配置您的 mvc 应用程序以验证访问令牌(不仅仅是颁发它们),如下所示:

// Adds IdentityServer
app.UseIdentityServer();

// Accept access tokens from identity server
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
    Authority = "http://localhost:5000",
    RequireHttpsMetadata = false,
    AuthenticationScheme = "Bearer"

    ApiName = "api1"
});

这样您就可以通过身份服务器为您的本地 api 请求访问令牌,(您还需要为 'api1' 配置一个 api 范围)。

要在您的 api-controller 中验证它们,请将其添加到您的控制器中:

    [Authorize(ActiveAuthenticationSchemes = "Bearer")]
    [HttpGet]
    public string Bearer()
    {
        return "Access Token Accepted";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2020-10-06
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多