Web Api .net framework 4.6.1 and identityServer4

3

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 framework 4.6.1 制作的 API 网站。 我希望从前端接收认证,并使用认证服务器验证访问权限。

针对这种情况,应如何进行设置?

2个回答

5

您的API应该在身份服务器中注册为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 时,它会响应“没有匹配当前请求的操作。路由值:[“controller=connect”,“action=accesstokenvalidation”]”,因为在 IdentityServer4 中未实现该端点。 - stefano
谢谢您。我已经卡在这里很久了!ValidationMode.Both解决了问题。 - Nimish David Mathew

2
@m3n7alsnak3的答案是正确的,我使用了app.UseIdentityServerBearerTokenAuthentication方法,但是我收到了未授权的消息!
我的WebAPI是.NET 4.6.1,我的IdentityServer4是.NET Core 3.1。
经过两天的研究和阅读IdentityServerBearerTokenValidationMiddleware源代码,我发现了这些文章: 需要设置IdentityServerOptions.AccessTokenJwtType 需要设置IdentityServerOptions.EmitLegacyResourceAudienceClaim 最后,我在我的IdentityServer配置中添加了这些选项,它可以正常工作。
var builder = services.AddIdentityServer(option =>
        {
            option.AccessTokenJwtType = "";
            option.EmitLegacyResourceAudienceClaim = true;
        })
        .AddInMemoryApiResources(Config.Apis)
        .AddInMemoryClients(Config.Clients);

1
我正在尝试同样的事情,但是出现了未经授权的错误。你能分享一下你的配置吗? - DevSay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接