配套源码:https://gitee.com/jardeng/IdentitySolution

 

本篇将创建使用[Code-授权码]授权模式的客户端,来对受保护的API资源进行访问。

1、接上一篇项目,因为之前创建IdentityServer认证服务器没有使用IdentityServer4提供的模板,在Code授权码模式就没有进行登录、授权的界面,所以重新创建一下IdentityServer项目。

重新使用IdentityServer4模板 - is4inmem创建项目。

ASP.NET Core3.1使用IdentityServer4中间件系列随笔(五):创建使用[Code-授权码]授权模式的客户端

ASP.NET Core3.1使用IdentityServer4中间件系列随笔(五):创建使用[Code-授权码]授权模式的客户端

将之前IdentityServer认证服务器Config.cs复制到新建的IdentityServer服务器即可,最后的IdentityServer认证服务器项目结构为:

ASP.NET Core3.1使用IdentityServer4中间件系列随笔(五):创建使用[Code-授权码]授权模式的客户端

然后在IdentityServer项目Config.cs中添加一个返回身份资源的方法

ASP.NET Core3.1使用IdentityServer4中间件系列随笔(五):创建使用[Code-授权码]授权模式的客户端

然后在IdentityServer项目Config.cs中添加一个客户端

ASP.NET Core3.1使用IdentityServer4中间件系列随笔(五):创建使用[Code-授权码]授权模式的客户端

注意:localhost:6001指的是我们将要创建的MVC客户端的项目地址,并非IdentityServer认证服务器的地址

/// 授权码模式(Code)
///     适用于保密客户端(Confidential Client),比如ASP.NET MVC等服务器端渲染的Web应用
new Client
{
    ClientId = "mvc client",
    ClientName = "ASP.NET Core MVC Client",

    AllowedGrantTypes = GrantTypes.Code,
    ClientSecrets = { new Secret("mvc secret".Sha256()) },

    RedirectUris = { "http://localhost:6001/signin-oidc" },
    FrontChannelLogoutUri = "http://localhost:6001/signout-oidc",
    PostLogoutRedirectUris = { "http://localhost:6001/signout-callback-oidc" },

    AlwaysIncludeUserClaimsInIdToken = true,
    AllowOfflineAccess = true,
    AllowedScopes =
    {
        "api1",
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        IdentityServerConstants.StandardScopes.Address,
        IdentityServerConstants.StandardScopes.Phone
    }
}
View Code

相关文章:

  • 2021-07-17
  • 2021-07-20
  • 2021-09-18
  • 2022-02-12
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-06-27
相关资源
相似解决方案