zoukankan      html  css  js  c++  java
  • .net core 中 identity server 4 之Server简单示例

    Steps:

    • 1.新建一个ASP.NET Core Web项目,SigmalHex.IdentityServer;

    • 2.安装包

    Install-Package IdentityServer4
    
    • 3.Startup中加入
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddInMemoryApiResources(Config.GetApiResources())
                .AddInMemoryClients(Config.GetClients());
        }
    
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(LogLevel.Debug);
            app.UseDeveloperExceptionPage();
    
            app.UseIdentityServer();
        }
    }
    
      • AddIdentityServer
        注册Identity Server 服务到DI系统。
      • AddTemporarySigningCredential
        每次启动的时候,生成一个临时的签名证书
    • 4.Config
    using IdentityServer4.Models;
    using System.Collections.Generic;
    
    namespace SigmalHex.IdentityServer
    {
        public class Config
        {
            // scopes define the API resources in your system
            public static IEnumerable<ApiResource> GetApiResources()
            {
                return new List<ApiResource>
                {
                    new ApiResource("api1", "My API")
                };
            }
    
            // client want to access resources (aka scopes)
            public static IEnumerable<Client> GetClients()
            {
                return new List<Client>
                {
                    new Client
                    {
                        ClientId = "client",
                        AllowedGrantTypes = GrantTypes.ClientCredentials,
    
                        ClientSecrets =
                        {
                            new Secret("secret".Sha256())
                        },
                        AllowedScopes = { "api1" }
                    }
                };
            }
        }
    }
    
    • 5.启动,即完成了IdentityServer内存版的搭建。
    using Microsoft.AspNetCore.Hosting;
    using System;
    using System.IO;
    
    namespace SigmalHex.IdentityServer
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.Title = "IdentityServer";
    
                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseUrls("http://localhost:5000")
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup<Startup>()
                    .Build();
    
                host.Run();
            }
        }
    }
    
  • 相关阅读:
    很有意思的“老黄历”网站
    ubuntu
    getopt在Python中的使用
    系统变量TERM不知是用来干什么的?它的值有vt100,vt220等,这些值代表什么意思?
    >/dev/null 2>&1
    linux下常用的ftp服务器软件
    Windows环境下访问NFS
    linux iSCSI target配置全过程
    iSCSI target在安全方面相关设定
    folly学习心得
  • 原文地址:https://www.cnblogs.com/pengzhen/p/7085908.html
Copyright © 2011-2022 走看看