首页 > 解决方案 > 带有 IdentityServer 的 ServiceStack

问题描述

使用https://github.com/NetCoreTemplates/mvcidentityserver上的模板 并尝试要求授权以访问 ServiceStack“Hello”对象。

我能够检索令牌并调用

https://localhost:5001/servicestack-identity

https://localhost:5001/webapi-identity

从 PostMan 成功。

但是,如果我添加一个 Authenticate 属性

namespace MyApp.ServiceInterface
{
    public class MyServices : Service
    {
        [Authenticate]
        public object Any(Hello request)
        {
            return new HelloResponse { Result = $"Hello, {request.Name}!" };
        }

对于 MyServices 中的 Any 方法,我无法使用令牌调用 /Hello。

/你好失败

/requiresauth 给出相同的结果

/需要身份验证

标签: servicestackidentityserver4

解决方案


我可以通过将以下内容添加到 MyApp.Startup.ConfigureServices()...

  services.AddAuthentication("Bearer")
                .AddJwtBearer("Bearer", options => {
                    options.Authority = "https://localhost:5000";
                    options.RequireHttpsMetadata = false;

                    options.Audience = "api1";
                });

推荐阅读