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();
            }
        }
    }
    
  • 相关阅读:
    高效编写微信小程序
    故事怎么讲才有逼格?
    基于RESTful API 怎么设计用户权限控制?
    【开源访谈】腾讯贺嘉:从小程序谈起,开发者该如何跟进新技术?
    图标字体设计
    微信小程序即将上线,创业者机会在哪里?
    微信小程序开发学习资料
    PC 微信扫码登陆
    一张二维码同时集成微信、支付宝支付
    支付宝Wap支付你了解多少?
  • 原文地址:https://www.cnblogs.com/pengzhen/p/7085908.html
Copyright © 2011-2022 走看看