栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

创建IdentityServer服务器

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建IdentityServer服务器

创建IdentityServer服务器:

在visual Studio中创建新的项目。(如果存在包不存在的问题,需要在visual Studio中下载对应包)。
创建项目流程参考链接:ASP.NET Core3.1使用IdentityServer4实现授权登录(一)

注:此项目ASP.NET Core 3.1版本,IdentityServer 3.1.2

补充说明(一)IdentityServer配置文件Config.cs内容
public class IdpConfig
    {
        /// 
        /// 用户认证信息
        /// 
        /// 
        public static IEnumerable GetApiResources()
        {
            return new List
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Address(),
                new IdentityResources.Email(),
                new IdentityResources.Phone()
            };
        }
        /// 
        /// API 资源
        /// 
        /// 
        public static IEnumerable GetApis()
        {
            return new List
            {
                new ApiResource("api1", "My API") 
            };
        }

        /// 
        /// 客户端应用
        /// 
        /// 
        public static IEnumerable GetClients()
        {
            return new List
            {
                new Client
                {
                    // 客户端ID 这个很重要
                    ClientId = "client",
                    //AccessToken 过期时间,默认3600秒,注意这里直接设置5秒过期是不管用的,解决方案继续看下面 API资源添加JWT
                    //AccessTokenLifetime=5, 
                    // 没有交互性用户,使用 clientid/secret 实现认证。
                    AllowedGrantTypes = GrantTypes.ClientCredentials, 
                    // 用于认证的密码
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    // 客户端有权访问的范围(Scopes)
                    AllowedScopes = { "api1" }
                }
            };
        }
    }
补充说明(二)startup.cs内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WFinancial
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
            })
                .AddDeveloperSigningCredential()//解决Keyset is missing 错误
                                                //.AddTestUsers(TestUsers.Users)
                                                //.AddInMemoryIdentityResources(IdpConfig.GetApiResources())
                .AddInMemoryApiResources(IdpConfig.GetApis())
                .AddInMemoryClients(IdpConfig.GetClients());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseIdentityServer();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
}

补充说明(三)启动——就绪


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292316.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号