IdentityServer4登录后如何重定向流程
IdentityServer4 how to redirect the flow after login
我已经安装了 IdentityServer4 和客户端(混合 Mvc 客户端)。一切正常。以下流程有效:
1. 用户调用安全页面PageX(控制器受Authorize属性保护)
2. 系统将流程重定向到 IdentityServer
上的登录页面
3. authentication/authorization 之后,IdentityServer 将用户重定向到 url 在客户端配置(名为 Home 的页面)中定义的 (redirect_uri)。
现在我不知道如何在步骤 3 中实现重定向到请求的原始页面的 PageX。
我必须创建自定义 AuthorizeAttribute 以在会话存储中保存 PageX 的 url,而不是在回调页面中使用它?或者 IdentityServer 或客户端上是否有任何配置可以帮助我?
提前致谢
这通常是您使用状态参数的目的。您的回调将收到未更改的状态值,然后您可以验证其中的 URL 是本地的并自动重定向到它。
我建议使用 .net 中的 DataProtection 功能来保护值不被篡改。
成功登录后,默认情况下,IdentityServer 中间件会尝试重定向到同意页面,通知用户 "allowed scopes"。在此页面中显示了客户端 mvc 站点将获得访问权限的声明:用户标识符、用户配置文件、电子邮件等。
如果您没有这样设置,您可以在定义 MVC 客户端时设置:"RequireConsent = false"。在这种情况下,IdentityServer 将在不显示同意页面的情况下重定向回 "RedirectUris"。
示例:
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "mvc",
ClientName = "mvc Client",
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email
},
RequireConsent = false
}
};
}
我在 IdentityServer4 演示和快速入门中注意到的另一件事是您需要以下 NuGet 包:
对于客户网站:
身份模型,
Microsoft.AspNetCore.All
对于 IdentityServer 身份验证应用程序:
身份服务器4,
IdentityServer4.AccessTokenValidation,
IdentityServer4.AspNetIdentity,
Microsoft.AspNetCore.All
您可以安装这些软件包只是为了让演示正常运行。
我已经安装了 IdentityServer4 和客户端(混合 Mvc 客户端)。一切正常。以下流程有效:
1. 用户调用安全页面PageX(控制器受Authorize属性保护)
2. 系统将流程重定向到 IdentityServer
上的登录页面
3. authentication/authorization 之后,IdentityServer 将用户重定向到 url 在客户端配置(名为 Home 的页面)中定义的 (redirect_uri)。
现在我不知道如何在步骤 3 中实现重定向到请求的原始页面的 PageX。
我必须创建自定义 AuthorizeAttribute 以在会话存储中保存 PageX 的 url,而不是在回调页面中使用它?或者 IdentityServer 或客户端上是否有任何配置可以帮助我?
提前致谢
这通常是您使用状态参数的目的。您的回调将收到未更改的状态值,然后您可以验证其中的 URL 是本地的并自动重定向到它。
我建议使用 .net 中的 DataProtection 功能来保护值不被篡改。
成功登录后,默认情况下,IdentityServer 中间件会尝试重定向到同意页面,通知用户 "allowed scopes"。在此页面中显示了客户端 mvc 站点将获得访问权限的声明:用户标识符、用户配置文件、电子邮件等。 如果您没有这样设置,您可以在定义 MVC 客户端时设置:"RequireConsent = false"。在这种情况下,IdentityServer 将在不显示同意页面的情况下重定向回 "RedirectUris"。
示例:
public static IEnumerable<Client> GetClients()
{
return new List<Client>
{
new Client
{
ClientId = "mvc",
ClientName = "mvc Client",
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email
},
RequireConsent = false
}
};
}
我在 IdentityServer4 演示和快速入门中注意到的另一件事是您需要以下 NuGet 包: 对于客户网站: 身份模型, Microsoft.AspNetCore.All
对于 IdentityServer 身份验证应用程序: 身份服务器4, IdentityServer4.AccessTokenValidation, IdentityServer4.AspNetIdentity, Microsoft.AspNetCore.All
您可以安装这些软件包只是为了让演示正常运行。