【问题标题】:Web Api .net framework 4.6.1 and identityServer4Web Api .net 框架 4.6.1 和 identityServer4
【发布时间】:2018-03-01 01:03:16
【问题描述】:

Web Api .net 框架

我使用 IdentityServer4 .net core 1.1 完成了身份验证服务。 客户端设置如下:

new Client
{
    ClientId = "client",
    AllowedGrantTypes = GrantTypes.ClientCredentials,

    ClientSecrets = 
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = { "api1" }
},

// resource owner password grant client
new Client
{
    ClientId = "ro.client",
    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,

    ClientSecrets = 
    {
        new Secret("secret".Sha256())
    },
    AllowedScopes = { "api1" }
},

// OpenID Connect hybrid flow and client credentials client (MVC)
new Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

    RequireConsent = true,

    ClientSecrets = 
    {
        new Secret("secret".Sha256())
    },

    RedirectUris = { "http://localhost:5002/signin-oidc" },
    PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        "api1"
    },
    AllowOfflineAccess = true
},

// JavaScript Client
new Client
{
    ClientId = "js",
    ClientName = "JavaScript Client",
    AllowedGrantTypes = GrantTypes.Implicit,
    AllowAccessTokensViaBrowser = true,

    RedirectUris = { "http://localhost/web/main.html#/redirectLogin#" },
    PostLogoutRedirectUris = { "http://localhost/web" },
    AllowedCorsOrigins = { "http://localhost" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        "api1"
    },

    RequireConsent = false
}

我有一个使用 oidc-client 的带有 javascript 的前端应用程序。 在其中,我可以使用以下设置向身份验证服务器进行身份验证:

var userManagerConfig = {
    authority: "http://localhost:5000",
    client_id: "js",
    redirect_uri: "http://localhost/web/main.html#/redirectLogin#",
    response_type: "id_token token",
    scope: "openid profile api1",
    post_logout_redirect_uri: "http://localhost/web",
};

var userManager = new Oidc.UserManager(userManagerConfig);

我还有一个用 .net 框架 4.6.1 制作的 api web。 在其中我想从前端接收身份验证并使用身份验证服务器来验证访问。

这种情况应该如何设置?

【问题讨论】:

    标签: c# asp.net-web-api2 identityserver4


    【解决方案1】:

    您的 API 应在 Identity Server 中注册为 API 资源。然后 - 它应该实现 OwinStartup 并在其中包含:

     public void Configuration(IAppBuilder app)
        {
            // accept access tokens from identityserver and require a scope of 'api1'
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = "<ids address>",
                ValidationMode = ValidationMode.Both,
    
                RequiredScopes = new[] { "myapi" }
            });
    
            // configure web api
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
    
            app.UseWebApi(config);
        }
    

    而且,因为是.NET Framework API,所以需要引用IdentityServer3.AccessTokenValidation。这不应该打扰您并引起任何担忧。它毫不犹豫地处理 IdentityServer4 令牌。

    其他一切都是标准的 - 你需要 AuthorizeAttribute 在你想要的所有控制器/方法上或添加这个:

            // require authentication for all controllers
            config.Filters.Add(new AuthorizeAttribute());
    

    Startup.cs 中对所有控制器强制授权。

    【讨论】:

    • 我也有这个问题。我尝试了您的解决方案,但 api 客户端仅适用于 IdentityServer3。使用 IdentityServer4 它响应 No actions matched the current request. Route values: ["controller=connect", "action=accesstokenvalidation"] 因为端点没有在 IdentityServer4 中实现
    • 谢谢。我一直被困在这上面! ValidationMode.Both 成功了。
    【解决方案2】:

    @m3n7alsnak3 的回答是正确的,我使用app.UseIdentityServerBearerTokenAuthentication 方法,但我收到未授权消息!

    我的 webapi 是 .net 4.6.1,我的 IdentityServer4 是 .Net Core 3.1。

    经过两天的研发和阅读IdentityServerBearerTokenValidationMiddleware源码,我找到了这篇文章:

    Need to set IdentityServerOptions.AccessTokenJwtType

    Need to set IdentityServerOptions.EmitLegacyResourceAudienceClaim

    最后我在我的IdentityServer 配置中添加了这个选项,它工作正常。

    var builder = services.AddIdentityServer(option =>
            {
                option.AccessTokenJwtType = "";
                option.EmitLegacyResourceAudienceClaim = true;
            })
            .AddInMemoryApiResources(Config.Apis)
            .AddInMemoryClients(Config.Clients);
    

    【讨论】:

    • 我正在尝试同样的事情,但得到一个未经授权的错误。你能分享你的配置吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2018-07-23
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2011-06-16
    • 2023-02-04
    相关资源
    最近更新 更多