2016 - 2024

感恩一路有你

CORE OAuth之identityServer4服务中心

浏览量:2270 时间:2024-01-24 07:37:57 作者:采采

在本文中,我们将介绍如何使用identityServer4实现 CORE的OAuth服务中心。你可以通过以下链接下载示例代码:

_VUv-l0GFgiw

密码:rfxx11*

新建 CORE WEB空项目

首先,我们需要新建一个 Core Web空项目。

安装identityserver4包

接下来,我们需要安装identityserver4包。在Visual Studio中,打开NuGet包管理器控制台,并运行以下命令:

Install-Package IdentityServer4

创建identityconfig配置帮助类

我们需要创建一个名为IdentityConfig的配置帮助类,用于定义可访问的对象和客户端信息。

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace identityserverpeizhi
{
    public class IdentityConfig
    {
        // 可访问的对象
        public static IEnumerable GetResource()
        {
            return new List
            {
                new ApiResource("apiServer","apiServer")
            };
        }
        public static IEnumerable GetClients()
        {
            return new List
            {
                new Client()
                {
                    ClientId  "clientId",
                    AllowedGrantTypes  ,
                    ClientSecrets  { new Secret("secret1122".Sha512()) },
                    AllowedScopes { "apiServer" } //可以访问的resource
                }
            };
        }
    }
}

注入identity配置

在ConfigureServices方法中,我们需要注入identityServer4的配置。

public void ConfigureServices(IServiceCollection services)
{
    // 依赖注入的配置
    ()
        .AddDeveloperSigningCredential()
        .AddInMemoryApiResources(())
        .AddInMemoryClients(())
        .AddMvc();
}

配置identity管道

在Configure方法中,我们需要配置identityServer4的管道。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (())
    {
        ();
    }
    ();
}

完成上述步骤后,你可以运行项目,并在浏览器中打开/.well-known/openid-configuration来验证服务是否正确工作。

版权声明:本文内容由互联网用户自发贡献,本站不承担相关法律责任.如有侵权/违法内容,本站将立刻删除。