当前位置: 移动技术网 > IT编程>开发语言>.net > IdentityServer4 密码模式认证

IdentityServer4 密码模式认证

2019年04月05日  | 移动技术网IT编程  | 我要评论

网络营销教程,同桌的你mp3下载,河南豫剧抬花轿

 授权服务器设置  

添加用户

  添加测试用户,也可以从数据库查  

      

public static list<testuser> gettestuser()
        {
            return new list<testuser>() {
                new testuser(){
                    subjectid = "1",
                    username ="zps",
                    password = "zps",
                    claims = new list<claim>(){
                        new claim("role","zps"),
                        new claim("aaa","asdasdsd"),
                    }
                },
                 new testuser(){
                    subjectid = "2",
                    username ="admin",
                    password = "admin",
                     claims = new list<claim>(){
                        new claim("role","admin")
                    }
                }
            };
        }
添加api资源                                                                                                                            

   添加api资源 ,api的key要和注册的client的api要匹配

  public static ienumerable<apiresource> getresource()
        {
            return new list<apiresource>(){
                new apiresource("api","my api")
            };
        }

 

 

添加客户端

 

  1.    客户端模式
  2.    密码模式
  3.    授权码模式
  4.    混合模式

    授权码模式和mvc模式的时候    这两个模式先不管

         //请求确认

               requireconsent = false,   这个属性要注意  如果是true  会先跳转到确认页面 然后再跳转到redirecturis

 public static ienumerable<client> getclients()
        {
            return new list<client>(){
                new client(){
                    clientid="client",
                    //客户端模式
                     allowedgranttypes=granttypes.clientcredentials,
                     clientsecrets={new secret("secret".sha256())},
                     allowedscopes={"api"}
                },
                new client(){
                    clientid="pwdclient",
                    //oauth密码模式
                     allowedgranttypes=granttypes.resourceownerpassword,
                     clientsecrets={new secret("secret".sha256())},
                     allowedscopes={"api"}
                },
                new client
                {
                   clientid = "mvc",
                   clientname = "mvc client",
                   allowedgranttypes = granttypes.hybrid,
                   clientsecrets =
                   {
                       new secret("secret".sha256())
                   },
                   // where to redirect to after login
                   redirecturis = { "http://localhost:5001/signin-oidc" },
                   requireconsent = false,
                   allowofflineaccess = true,
                    // where to redirect to after logout
                    postlogoutredirecturis = { "http://localhost:5001/signout-callback-oidc" },

                     allowedscopes = new list<string>
                  {
                    identityserverconstants.standardscopes.openid,
                    identityserverconstants.standardscopes.profile,
                  }
                },
                new client
                {
                   clientid = "js",
                    clientname = "javascript client",
                    allowedgranttypes = granttypes.code,
                    requirepkce = true,
                    requireclientsecret = false,

                    redirecturis =           { "http://localhost:5003/callback.html" },
                    postlogoutredirecturis = { "http://localhost:5003/" },
                    allowedcorsorigins =     { "http://localhost:5003" },
                    requireconsent = false,
                    allowedscopes =
                    {
                        identityserverconstants.standardscopes.openid,
                        identityserverconstants.standardscopes.profile,
                        "api"
                    }
                }
            };
        }

 

 

 

添加identityserver 保护的资源

 

    可以自定义claim

 public static ienumerable<identityresource> getidentityresources()
        {
            return new identityresource[]
            {
                new identityresources.openid(),
                new identityresources.profile(),
            };
        }

 

 

把identityserver注入到容器

 

  .adddevelopersigningcredential() 生成token 需要的密钥和公钥  正式环境需要换成正经的 

     o.userinteraction.loginurl = "/auth/login";

          o.userinteraction.logouturl = "/auth/logout";

   o.userinteraction.errorurl = "/auth/error";
这三个是混合模式需要的 登录的地址 登出的地址 授权失败的地址
services.addidentityserver(o =>
            {
                o.userinteraction.loginurl = "/auth/login";
                o.userinteraction.logouturl = "/auth/logout";
                o.userinteraction.errorurl = "/auth/error";
            })
                    .addinmemoryidentityresources(config.getidentityresources())
                    .adddevelopersigningcredential()
                    .addinmemoryclients(config.getclients())
                    .addinmemoryapiresources(config.getresource())
                    .addtestusers(config.gettestuser());

 

    configure把中间件加到netcore中

app.useidentityserver();

postman测试

  1.   grant-type:密码模式对应 password 
  2.        username 用户名
  3.       password  密码
  4.      client_id 客户端id  对应 授权服务clientid
  5.      client_secret  客户端secret

 

源码

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网