IdentityServer4入门三:受权模式

在入门1、入门二咱们实现了一个完整的API保护的过程。须要保护的API只需在其Controler上应用[Authorize]特性,来显式指定受保护的资源。而咱们实现的这个例子,所应用的模式叫“Client Credentials”,在Config.cs中有这么一段代码html

 

 

 

“Client Credentials”(客户端凭证)模式,是最简单的受权模式,由于受权的流程仅发生在Client与Identity Server之间。web

该模式的适用场景为服务器与服务器之间的通讯。好比对于一个电子商务网站,将订单和物流系统分拆为两个服务分别部署。物流系统须要获取须要派送的物品,而订单系统须要跟踪物流信息。而这两个系统之间服务的受权就能够经过这种模式来实现。api

官方客户受权几种说明https://identityserver4.readthedocs.io/en/latest/topics/clients.html浏览器

OAuth2.0 定义了四种受权模式:服务器

  1. Implicit:简化模式;直接经过浏览器的连接跳转申请令牌。
  2. Client Credentials:客户端凭证模式;该方法一般用于服务器之间的通信;该模式仅发生在Client与Identity Server之间。
  3. Resource Owner Password Credentials:密码模式
  4. Authorization Code:受权码模式;

Implicit

就是认证服务器提供网页进行登陆。受保护的网站使用该模式,访问须要受权的网页时若是没认证的会自动跳转到认证服务器的登陆界面,登陆后自动回到原来访问的受权页面。mvc

Resource Owner Password Credentials

用户须要向客户端提供本身的用户名和密码。客户端使用这些信息,向"服务商提供商"索要受权。ide

在这种模式中,用户必须把本身的密码给客户端,这一般用在用户对客户端高度信任的状况下,好比客户端是操做系统的一部分,或由一个著名公司出品。通常不建议使用该模式。认证服务器只有在其余受权模式没法执行的状况下,才应考虑使用这种模式。网站

 

 

Authorization Code

咱们使用第三方使用QQ账号进行登陆的例子说明spa

 

 

这有一篇不错的说明,上图也来自该网页
http://www.javashuo.com/article/p-nzebpclj-gb.html操作系统

 

一个认证服务器,能够应用两种模式吗?

能够的。后面的例子,咱们要为入门一的例子上加上Implicit模式。下面源码中的将是一个新的web的网站,使用44302端口

public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client
        {
            ClientId = "client",
            // no interactive user, use the clientid/secret for authentication
            AllowedGrantTypes = GrantTypes.ClientCredentials,
            // secret for authentication
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },
            // scopes that client has access to
            AllowedScopes = { "api1" }
        },
        new Client
        {
            ClientId = "mvc",
            ClientName = "MVC Client",            
            AllowedGrantTypes = GrantTypes.Hybrid,
            AllowOfflineAccess = true,
            ClientSecrets = { new Secret("secret".Sha256()) },
            RedirectUris =           { "https://localhost:44302/signin-oidc" },
            PostLogoutRedirectUris = { "https://localhost:44302/" },
            FrontChannelLogoutUri =  "https://localhost:44302/signout-oidc",
            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email,
                "api1", "api2.read_only"
            },
        }
    };
}

  

各个模式的Client源码,能够在这找
https://identityserver4.readthedocs.io/en/latest/topics/clients.html

相关文章
相关标签/搜索