Procházet zdrojové kódy

认证服务添加完成, 还需要加入前端的跨域访问

徐明 před 5 roky
rodič
revize
145b1a91b5

+ 3 - 2
CasinosManager.IdentityServer/CasinosManager.IdentityServer/AccountValidator.cs

@@ -12,8 +12,8 @@ namespace CasinosManager.IdentityServer
         {
             var claims = new Claim[]
             {
-                new Claim("USERID", userInfo.Id.ToString()),
-                new Claim("USERNAME", userInfo.UserName),
+                new Claim("UserId", userInfo.Id.ToString()),
+                new Claim("UserName", userInfo.UserName),
                 new Claim(JwtClaimTypes.Name,"test"),
                 new Claim(JwtClaimTypes.GivenName, "jaycewu"),
                 new Claim(JwtClaimTypes.FamilyName, "yyy"),
@@ -25,6 +25,7 @@ namespace CasinosManager.IdentityServer
 
         public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
         {
+            //todo 从数据库里查询验证
             Account accountResult = new Account();
             accountResult.User = new UserInfo();
             accountResult.User.Id = 1;

+ 34 - 0
CasinosManager.IdentityServer/CasinosManager.IdentityServer/ProfileService.cs

@@ -0,0 +1,34 @@
+using IdentityServer4.Models;
+using IdentityServer4.Services;
+using System;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace CasinosManager.IdentityServer
+{
+    public class ProfileService : IProfileService
+    {
+        public async Task GetProfileDataAsync(ProfileDataRequestContext context)
+        {
+            try
+            {
+                //depending on the scope accessing the user data.
+                var claims = context.Subject.Claims.ToList();
+
+                //set issued claims to return
+                context.IssuedClaims = claims.ToList();
+                context.AddRequestedClaims(claims);
+            }
+            catch (Exception ex)
+            {
+                //log your error
+            }
+
+        }
+
+        public async Task IsActiveAsync(IsActiveContext context)
+        {
+            context.IsActive = true;
+        }
+    }
+}

+ 3 - 2
CasinosManager.IdentityServer/CasinosManager.IdentityServer/Startup.cs

@@ -15,14 +15,15 @@ namespace CasinosManager.IdentityServer
              .AddDeveloperSigningCredential()
              .AddInMemoryApiResources(Config.GetApiResources())
              .AddInMemoryClients(Config.GetClients())
-             .AddResourceOwnerValidator<AccountValidator>();
+             .AddResourceOwnerValidator<AccountValidator>()
+             .AddProfileService<ProfileService>(); ;
              //.AddTestUsers(Config.GetUsers());
 
             services.AddCors(options =>
             {
                 options.AddPolicy("angular", policy =>
                 {
-                    policy.WithOrigins("http://localhost:3000")
+                    policy.WithOrigins("http://localhost:4200")
                             .AllowAnyHeader()
                             .AllowAnyMethod()
                             .AllowCredentials();