zoukankan      html  css  js  c++  java
  • IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤
    1.创建一个web项目,引入Identityserver4的nuget包
    2.新建一个类,实现IResourceOwnerPasswordValidator接口
    ···csharp
    public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
    {
    VerifyUserInputDto inputDto = new VerifyUserInputDto
    {
    UserName = context.UserName,
    Password = context.Password
    };
    var verifyResult = await _userService.VerifyUserPasswordAsync(inputDto);
    if (verifyResult.isSuccess)
    {
    context.Result = new GrantValidationResult(verifyResult.userInfo.UserId.ToString(), OidcConstants.AuthenticationMethods.Password);
    }
    else
    {
    //验证失败
    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential");
    }
    }

    大致就是读取数据库数据,与context.username ,password,进行比对,一致则通过,不一致就是失败。
    3.Starpup中增加
    ···csharp
    services.AddIdentityServer()
                      .AddInMemoryApiResources(你定义的资源)
                      .AddInMemoryClients(你定义的客户端)
                      .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>() //主要是user这里替换我们自己的服务
                      .AddDeveloperSigningCredential(); // rsa密钥,自动生产一个临时的。
    

    4.api项目中 配置好identityserver ,Post请求 identityserver 下connect/token 这个地址 获取token, 记得在设置token的时候加上 Bearer 前缀 否则token是无效的!!

  • 相关阅读:
    推销员问题
    string类实现
    链表倒数第k个节点
    设计模式之单例模式大全
    空类 sizeof 为什么是1
    类的三种继承方式
    单例模式典型创建方法(三种)
    虚函数实现
    链表删除结点
    TCP的状态转移
  • 原文地址:https://www.cnblogs.com/zzqvq/p/11180926.html
Copyright © 2011-2022 走看看