https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践
vscode 下面命令
dotnet new webapi -o is4ef
cd is4ef
dotnet add package IdentityServer4.EntityFramework
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
增加Config.cs
1 // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. 2 // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. 3 4 using IdentityServer4; 5 using IdentityServer4.Models; 6 using IdentityServer4.Test; 7 using System.Collections.Generic; 8 using System.Security.Claims; 9 10 namespace is4ef 11 { 12 public class Config 13 { 14 // scopes define the resources in your system 15 public static IEnumerable<IdentityResource> GetIdentityResources() 16 { 17 return new List<IdentityResource> 18 { 19 new IdentityResources.OpenId(), 20 new IdentityResources.Profile(), 21 }; 22 } 23 24 public static IEnumerable<ApiResource> GetApiResources() 25 { 26 return new List<ApiResource> 27 { 28 new ApiResource("api1", "My API") 29 }; 30 } 31 32 // clients want to access resources (aka scopes) 33 public static IEnumerable<Client> GetClients() 34 { 35 // client credentials client 36 return new List<Client> 37 { 38 new Client 39 { 40 ClientId = "client", 41 AllowedGrantTypes = GrantTypes.ClientCredentials, 42 43 ClientSecrets = 44 { 45 new Secret("secret".Sha256()) 46 }, 47 AllowedScopes = { "api1" } 48 }, 49 50 // resource owner password grant client 51 new Client 52 { 53 ClientId = "ro.client", 54 AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, 55 56 ClientSecrets = 57 { 58 new Secret("secret".Sha256()) 59 }, 60 AllowedScopes = { "api1" } 61 }, 62 63 // OpenID Connect hybrid flow and client credentials client (MVC) 64 new Client 65 { 66 ClientId = "mvc", 67 ClientName = "MVC Client", 68 AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 69 70 ClientSecrets = 71 { 72 new Secret("secret".Sha256()) 73 }, 74 75 RedirectUris = { "http://localhost:5002/signin-oidc" }, 76 PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" }, 77 78 AllowedScopes = 79 { 80 IdentityServerConstants.StandardScopes.OpenId, 81 IdentityServerConstants.StandardScopes.Profile, 82 "api1" 83 }, 84 AllowOfflineAccess = true 85 } 86 }; 87 } 88 89 public static List<TestUser> GetUsers() 90 { 91 return new List<TestUser> 92 { 93 new TestUser 94 { 95 SubjectId = "1", 96 Username = "alice", 97 Password = "password", 98 99 Claims = new List<Claim> 100 { 101 new Claim("name", "Alice"), 102 new Claim("website", "https://alice.com") 103 } 104 }, 105 new TestUser 106 { 107 SubjectId = "2", 108 Username = "bob", 109 Password = "password", 110 111 Claims = new List<Claim> 112 { 113 new Claim("name", "Bob"), 114 new Claim("website", "https://bob.com") 115 } 116 } 117 }; 118 } 119 } 120 }