首页 > 解决方案 > 即使添加到客户端,Identity Server 4 中也缺少电子邮件范围

问题描述

我正在使用带有 Angular 客户端的 Identity Server 4。

在双方我都添加相同的范围:

'openid profile offline_access email api'

但是我收到一个错误:

Sorry, there was an error : invalid_scope
Invalid scope

我检查了 Well-Known OpenID 配置,我看到:

"scopes_supported": [
  "openid",
  "profile",
  "api",
  "offline_access"

]

所以电子邮件范围不显示?

这可能是问题吗?为什么会这样?

我的 Identity Server 4 客户端定义是:

new Client {

  ClientId = "spa",
  ClientSecrets = { new Secret("secret".Sha256()) },
  AllowedGrantTypes = GrantTypes.Code,
  RequirePkce = true,
  RequireClientSecret = false,
  AllowAccessTokensViaBrowser = true,
  AllowOfflineAccess = true,

  AllowedScopes = { 
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile, 
    IdentityServerConstants.StandardScopes.Email, 
    IdentityServerConstants.StandardScopes.OfflineAccess,
    "api" 
  },  

  RedirectUris = new List<String> { 
    "https://localhost:5001",
    "https://localhost:5001/index.html",
    "https://localhost:5001/silent-refresh.html" 
  },

  PostLogoutRedirectUris = new List<String> { 
    "https://localhost:5001/" 
  },

  AllowedCorsOrigins = new List<String> { 
    "https://localhost:5001" 
  }

}

标签: identityserver4

解决方案


您需要添加一个 IdentityResource:

    public static IEnumerable<IdentityResource> GetIdentityResources()
    {
        return new List<IdentityResource>
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
            new IdentityResources.Email()
        };
    }

然后在 startup.cs 中添加:

var identityServerBuilder = services.AddIdentityServer(...)
.AddInMemoryIdentityResources(GetIdentityResources())

推荐阅读