zoukankan      html  css  js  c++  java
  • .Net Core WebApi(三)--使用 IdentityServer4 4.1.1 踩坑记录

    目的:创建IdentityServer 并通过PostMan验证获取token

    第一次配置如下
     public class Config
        {
            public static IEnumerable<ApiResource> GetResource()
            {
                return new List<ApiResource>
                  { new ApiResource("api","My Api") };
            }
    
            public static IEnumerable<Client> GetClients()
            {
                return new List<Client> { new Client()
                {
                ClientId="client",
                AllowedGrantTypes=GrantTypes.ClientCredentials,
                ClientSecrets={ new Secret("secret".Sha256())},
                AllowedScopes={"api"}
                } };
            }
        }
    

    PostMan的请求如图

    http://localhost:5000/connect/token

    结果显示 请求无效 参照教程 >https://www.cnblogs.com/monster17/p/13261647.html 得知是因为传参格式的问题

    • 1.我们将Body改为 格式
    • 2.加参数<AllowedScopes: api> 【指定的api为配置Config中apiresource的名称】 再次请求
      如图

    结果又显示scope无效 【这里懵逼了很久 明明Config中apiResource的name 和client中AllowedScopes的接口是一致的】
    但是参照报错 一定是scop的问题 于是去找了一下官方文档关于API Resources 的使用

    https://identityserver4.readthedocs.io/en/latest/topics/resources.html?highlight=apiresource#api-resources

    参照原文

    这里也便可以理解 问题所在了。 apiResource并不等同于scope 它相当于是scopes集合的统称 实现了scope的分组【但具体的scope还是要进行设置的 我们先前的Config 缺少具体scope =。= 真是令人头大】

    先参照例子理解一下

    //创建多个scope
    {
        return new List<ApiScope>
        {
            // invoice API specific scopes
            new ApiScope(name: "invoice.read",   displayName: "Reads your invoices."),
            new ApiScope(name: "invoice.pay",    displayName: "Pays your invoices."),
    
            // customer API specific scopes
            new ApiScope(name: "customer.read",    displayName: "Reads you customers information."),
            new ApiScope(name: "customer.contact", displayName: "Allows contacting one of your customers."),
    
            // shared scope
            new ApiScope(name: "manage", displayName: "Provides administrative access to invoice and customer data.")
        };
    }
    
    //调取时 将多个scope的Name加参时 可能会很长很长 为了方便 我们将scope 进行分组 后期可直接调用**统称** 这里分成两组
    public static readonly IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("invoice", "Invoice API")//设置每组scopes的统称
            {
                Scopes = { "invoice.read", "invoice.pay", "manage" }//设置每组scope具体名称
            },
    
            new ApiResource("customer", "Customer API")
            {
                Scopes = { "customer.read", "customer.contact", "manage" }
            }
        };
    }
    

    再来回头看看我们的Config 修改如下

        public class Config
        {
    
            public static IEnumerable<ApiResource> GetApiResources()
            {
                return new List<ApiResource>() { 
                    new ApiResource("api", "My api") { Scopes={"api1","api2"} //将具体的scopes 归为一组 统称为api
                } };
            }
            //创建具体的scope
            public static IEnumerable<ApiScope> GetApiScopes()
            {
                return new List<ApiScope> { 
                    new ApiScope("api1","My first api"),
                    new ApiScope("api2","My second api")};
            }
    
            public static IEnumerable<Client> GetClients()
            {
                return new List<Client>() { new Client {
                ClientId="client",
                AllowedGrantTypes=GrantTypes.ClientCredentials,
                ClientSecrets={new Secret("secret".Sha256()) },
                AllowedScopes={ "api1","api2"}//此处一定要配置具体的scope名称 外部调用可直接配置为 api【统称】
                } };
            }
    

    用PostMan验证一下

    成功了 emmmm 又是美好的一天【还是要多读源文档e =.=】

    补充 获取Refresh_token

    GetClients配置修改如下

    结果

  • 相关阅读:
    python主要探索函数
    数据分析分析方法
    监控hadoop任务结果shell脚本
    shell编程
    hadoop介绍
    数据探索
    Python数据分析简介
    数据挖掘基础篇之整体思路
    sqlAlchemy
    python md5 加密
  • 原文地址:https://www.cnblogs.com/Alicia-meng/p/13915748.html
Copyright © 2011-2022 走看看