美文网首页
IdentityServer4 接入自己的用户体系

IdentityServer4 接入自己的用户体系

作者: wwmin_ | 来源:发表于2021-11-04 18:31 被阅读0次

IdentityServer4提供的demo 是用的自己的表结构,但是对于我们来说就不是很适用了,研究了下他的源码发现他的密码模式,大概就是更改下面几个方法大,致就是读取数据库数据,与context.username ,password,进行比对,一致则通过,不一致就是失败

public class ProfileService : IProfileService {
       //services
       private IUserRepository _userRepository = new UserRepository();
 
 
       //build claims array from user data
       public static Claim[] GetUserClaims(User user)
       {
           var c = new Claim[] { };
           c[0] = new Claim("user_id", "465464");
           c[1] = new Claim("sub", "465464");           
           return c;
       }
 
       /// <summary>
       /// 每当请求有关用户的声明时(例如,在令牌创建期间或通过userinfo端点),都会调用此方法
       /// </summary>
       /// <param name="context"></param>
       /// <returns></returns>
       public async Task GetProfileDataAsync(ProfileDataRequestContext context)
       {
           try
           {
               //depending on the scope accessing the user data.
               if (!string.IsNullOrEmpty(context.Subject.Identity.Name))
               {
                   //get user from db (in my case this is by email)
                   var user = await _userRepository.FindAsync(45121);
 
                   if (user != null)
                   {
                        var claims = GetUserClaims(user);
 
                       //set issued claims to return
                       context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                   }
               }
               else
               {
                   //get subject from context (this was set ResourceOwnerPasswordValidator.ValidateAsync),
                   //where and subject was set to my user id.
                   var userId = context.Subject.Claims.FirstOrDefault(x => x.Type == "sub");
 
                   if (!string.IsNullOrEmpty(userId?.Value) && long.Parse(userId.Value) > 0)
                   {
                       //get user from db (find user by user id)
                       var user = await _userRepository.FindAsync(long.Parse(userId.Value));
 
                       // issue the claims for the user
                       if (user != null)
                       {
                           var claims = ResourceOwnerPasswordValidator.GetUserClaims(user);
 
                           context.IssuedClaims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList();
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               //log your error
           }
       }
 
       //check if user account is active.
       public async Task IsActiveAsync(IsActiveContext context)
       {
           try
           {
               //get subject from context (set in ResourceOwnerPasswordValidator.ValidateAsync),
               var userId = context.Subject.Claims.FirstOrDefault(x => x.Type == "user_id");
 
               if (!string.IsNullOrEmpty(userId?.Value) && long.Parse(userId.Value) > 0)
               {
                   var user = await _userRepository.FindAsync(long.Parse(userId.Value));
 
                   if (user != null)
                   {
                       if (user.IsActive)
                       {
                           context.IsActive = user.IsActive;
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               //handle error logging
           }
       }
   }
public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator {
        private IUserRepository _userRepository = new UserRepository();
        //build claims array from user data
        //build claims array from user data
        public static Claim[] GetUserClaims(User user)
        {
            var c = new Claim[] { };
            c[0] = new Claim("user_id", "465464");
            c[1] = new Claim("sub", "465464");
            return c;
        }
        //this is used to validate your user account with provided grant at /connect/token
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            try
            {
                //get your user model from db (by username - in my case its email)
                var user = await _userRepository.FindAsync(1241);
                if (user != null)
                {
                    //check if password match - remember to hash password if stored as hash in db
                    if (true)
                    {
                        //set the result
                        context.Result = new GrantValidationResult(
                            subject: user.Id.ToString(),
                            authenticationMethod: "custom",
                            claims: GetUserClaims(user));
 
                        return;
                    }
 
                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Incorrect password");
                    return;
                }
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "User does not exist.");
                return;
            }
            catch (Exception ex)
            {
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "Invalid username or password");
            }
        }
    }
     //add identity server 4
             services.AddIdentityServer()
                   .AddInMemoryApiScopes(Config.GetApiScopes())
                    .AddInMemoryApiResources(Config.GetApiResources())
                   .AddInMemoryIdentityResources(Config.GetIdentityResources())
                    .AddInMemoryClients(Config.GetClients())
                    .AddDeveloperSigningCredential(persistKey: false)
                    .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()//用户校验
                .AddProfileService<ProfileService>();
image.png

参考资料:https://buildmedia.readthedocs.org/media/pdf/identityserver4/release/identityserver4.pdf

源码地址:https://github.com/imfrank/Galaxy.IdentityServer

相关文章

  • IdentityServer4 接入自己的用户体系

    IdentityServer4提供的demo 是用的自己的表结构,但是对于我们来说就不是很适用了,研究了下他的源码...

  • angular 接入 IdentityServer4

    angular 接入 IdentityServer4 Intro 最近把活动室预约的项目做了一个升级,预约活动室需...

  • IdentityServer4实现Token认证登录以及权限控制

    IdentityServer4实现Token认证登录以及权限控制 基于Token的安全验证体系 这个比较简单, 简...

  • 二十三种设计模式之外观模式

    外观模式是我在工作之中首次用到的一种设计模式,当时我正在接入公司内部的用户体系,将每一个需要调用的用户接口都封装成...

  • 承载网

    接入层:通常将网络中直接面向用户连接或访问网络的部分称为接入层,接入层目的是允许终端用户连接到网络,因此接入层交换...

  • 接入网光缆线路工程

    接入网(AN,Access Network)也称为用户接入网,接入网光缆线路是由业务节点接口(SNI)和相关用户网...

  • 新特SDK

    概述 如果你想要接入新特的用户程序接口,可以通过接入该SDK快速访问新特的用户程序,接口包含了用户信息管理,用户登...

  • 2019-01-27

    裂变是小程序江湖的核心技能 小程序即用即走的特性,极大降低了创业者连接用户的门槛。接入微信体系、省去下载动作后,每...

  • IdentityServer4(一)简介

    1、什么是IdentityServer4 IdentityServer4是依赖于asp.net core来实现op...

  • MySQL体系结构及运行机理

    mysql体系结构 关于mysql的整体体系结构如下: Client Connectors接入方 支持协议很多Ma...

网友评论

      本文标题:IdentityServer4 接入自己的用户体系

      本文链接:https://www.haomeiwen.com/subject/caicgltx.html