Kaynağa Gözat

认证服务完成

徐明 6 yıl önce
ebeveyn
işleme
dc09ff9f4c

+ 19 - 2
CasinosManager.IdentityServer/CasinosManager.IdentityServer/Config.cs

@@ -1,4 +1,5 @@
-using IdentityServer4.Models;
+using IdentityServer4;
+using IdentityServer4.Models;
 using IdentityServer4.Test;
 using System.Collections.Generic;
 
@@ -15,6 +16,16 @@ namespace CasinosManager.IdentityServer
             };
         }
 
+        public static IEnumerable<IdentityResource> GetIdentityResource()
+        {
+            return new List<IdentityResource>
+             {
+                 new IdentityResources.OpenId(),
+                 new IdentityResources.Profile(),
+                 new IdentityResources.Email()
+             };
+        }
+
         // clients want to access resources (aka scopes)
         public static IEnumerable<Client> GetClients()
         {
@@ -31,7 +42,13 @@ namespace CasinosManager.IdentityServer
                     {
                         new Secret("secret".Sha256())
                     },
-                    AllowedScopes = { "CasinosApi" }
+                    AllowedScopes =
+                    {
+                        "CasinosApi",
+                        IdentityServerConstants.StandardScopes.Profile,
+                        IdentityServerConstants.StandardScopes.OpenId,
+                        IdentityServerConstants.StandardScopes.OfflineAccess
+                    }
                 }
             };
         }

+ 21 - 0
CasinosManager.IdentityServer/CasinosManager.IdentityServer/CorsPolicyService.cs

@@ -0,0 +1,21 @@
+using IdentityServer4.Services;
+using System.Threading.Tasks;
+
+namespace CasinosManager.IdentityServer
+{
+    public class CorsPolicyService : ICorsPolicyService
+    {
+        public Task<bool> IsOriginAllowedAsync(string origin)
+        {
+            var task = Task.Run<bool>(() =>
+            {
+                if (origin == "http://localhost:4200")
+                {
+                    return true;
+                }
+                return false;
+            });
+            return task;
+        }
+    }
+}

+ 9 - 13
CasinosManager.IdentityServer/CasinosManager.IdentityServer/Startup.cs

@@ -14,21 +14,12 @@ namespace CasinosManager.IdentityServer
             services.AddIdentityServer()
              .AddDeveloperSigningCredential()
              .AddInMemoryApiResources(Config.GetApiResources())
+             .AddInMemoryIdentityResources(Config.GetIdentityResource())
              .AddInMemoryClients(Config.GetClients())
              .AddResourceOwnerValidator<AccountValidator>()
-             .AddProfileService<ProfileService>(); ;
+             .AddProfileService<ProfileService>()
+             .AddCorsPolicyService<CorsPolicyService>();
              //.AddTestUsers(Config.GetUsers());
-
-            services.AddCors(options =>
-            {
-                options.AddPolicy("angular", policy =>
-                {
-                    policy.WithOrigins("http://localhost:4200")
-                            .AllowAnyHeader()
-                            .AllowAnyMethod()
-                            .AllowCredentials();
-                });
-            });
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -40,7 +31,12 @@ namespace CasinosManager.IdentityServer
             }
 
             app.UseIdentityServer();
-            app.UseCors("angular");
+
+            app.UseCors(buider =>
+            {
+                buider.WithOrigins("http://localhost:4200")
+                .AllowAnyHeader();
+            });
 
             app.Run(async (context) =>
             {