【问题标题】:IdentityServer authentication for admin application in the same host同一主机中管理应用程序的 IdentityServer 身份验证
【发布时间】:2017-07-30 07:15:42
【问题描述】:

我有一个用于托管 IdentityServer3 的 ASP.NET MVC 应用程序,但我想在同一主机上托管 Angular + WebAPI 2 自定义管理应用程序。该管理应用程序正在使用 oidc-client 库进行身份验证。下面是我的 Startup 类,用于配置 IdentityServer 和调用 UseIdentityServerBearerTokenAuthentication 方法。如您所见,我在 async Task 中调用了该方法,因为这发生在 IdentityServer 启动之前不久。

身份验证有效,我的 Angular ajax 请求充满了有效的访问令牌,但我没有在 WebApi 控制器上收到任何声明。 ClaimsPrincipal 有空的 Claims 列表,并且 IsAuthenticated 为 false。 我的客户端配置也设置正确。这个设置有问题吗?

 public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Trace()
            .CreateLogger();


        var factory = new IdentityServerServiceFactory();

        factory.Register<IdentityDatabaseModel>(new Registration<IdentityDatabaseModel>(typeof(IdentityDatabaseModel)));
        factory.Register<UserDataService>(new Registration<UserDataService>(typeof(UserDataService)));
        factory.Register<TokenDataService>(new Registration<TokenDataService>(typeof(TokenDataService)));
        factory.Register<ClaimsDataService>(new Registration<ClaimsDataService>(typeof(ClaimsDataService)));
        factory.Register<ClientDataService>(new Registration<ClientDataService>(typeof(ClientDataService)));


        factory.UserService = new Registration<IUserService>(typeof(UserService));

        factory.RefreshTokenStore = new Registration<IRefreshTokenStore, RefreshTokenStore>();
        factory.ClientStore = new Registration<IClientStore, ClientStore>();


        factory.UseInMemoryScopes(WebApplication1.Models.IS.Scopes.Get());

        var options = new IdentityServerOptions
        {
            SigningCertificate = Certificate.Get(), 
            Factory = factory,
            RequireSsl = false,
            LoggingOptions = new LoggingOptions
            {
                //EnableHttpLogging = true,
                EnableKatanaLogging = true,
                EnableWebApiDiagnostics = true,
                WebApiDiagnosticsIsVerbose = true
            },
            EnableWelcomePage = false
        };
        app.UseIdentityServer(options);

        #region IdentityServer authentication
        JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();

        Task.Factory.StartNew(() => {
            System.Threading.Thread.Sleep(5000);
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "http://localhost:17343",
                RequiredScopes = new[] { "openid", "email", "roles", "profile" },
                ClientId = "lsidentity",
                ClientSecret = "secret"
            });
        });
        #endregion
    }
}

【问题讨论】:

  • 我认为您在 Web api 中使用了错误的范围。你的 web api 应该定义它自己的范围,它需要在访问令牌中定义你想要的声明,然后这些声明将被推送并在你的 web api 控制器中可用

标签: c# asp.net-mvc identityserver3


【解决方案1】:

问题是我需要在 WebApi 配置中配置 IssuerName 和 SigningCertificate,所以看起来像这样:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
        {
            Authority = "http://localhost:17343",
            //Authority = "http://192.168.254.3:303",
            RequiredScopes = new[] { "openid",
                "email", "profile" },
            IssuerName = "http://localhost:17343", //added this
            SigningCertificate = Certificate.Get(), // ...and this
            // client credentials for the introspection endpoint
            ClientId = "lsidentity",
            ClientSecret = "secret".Sha256()
        });

github上有个问题,一开始没找到。 https://github.com/IdentityServer/IdentityServer3.AccessTokenValidation/issues/38

也不需要将其称为任务,它现在可以正常工作了。

【讨论】:

    猜你喜欢
    • 2020-11-16
    • 2023-03-16
    • 1970-01-01
    • 2021-06-08
    • 2021-04-22
    • 2021-07-13
    • 1970-01-01
    • 2012-09-18
    • 2019-09-27
    相关资源
    最近更新 更多