我试图弄清楚服务器端客户端(MVC/ASP.NET Core 2)如何查询IdentityServer4以检索在前一个会话中创建的持久登录的各种声明范围
不提示登录
如果永久登录无效(用户不活动、cookie过期等)。
我们将隐式流与第三方身份验证(Google、FB等)结合使用,但我们在IdentityServer的
ExternalLoginCallback
.
访问上的声明
HttpContext.User
(我们是
不
使用ASP。NET Identity)在建立登录的会话期间非常有效。在以后的某个会话中,导航到具有
[Authorize]
属性也起作用:如果用户以前登录过,则他们可以透明地访问资源,填充声明等。如果没有,则会提示他们登录,这对于用户启动的操作来说是正常的。
然而,我们要求客户端登录页根据用户是匿名的还是经过身份验证的来更改内容。一个简单的例子是匿名用户的“注册”和“登录”链接,而经过身份验证的用户的“帐户”和“注销”链接。
因此,我们有理由检索声明并在有效的情况下启动持久登录,但在无效的情况下什么也不做(没有登录提示):我们不希望登录页强制每个匿名用户登录屏幕。
关于我们在管道两端的设置,没有什么特别要说的。客户:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:5000";
options.RequireHttpsMetadata = true;
options.ClientId = "example.com.webserver";
options.ClientSecret = "examplesecret";
options.ResponseType = "id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("example.com.identity");
});
IdentityServer客户端资源定义:
new Client
{
ClientId = "example.com.webserver",
ClientName = "example.com",
ClientUri = "https://localhost:5002",
AllowedGrantTypes = GrantTypes.Implicit,
ClientSecrets = {new Secret("examplesecret".Sha256())},
RequireConsent = false,
AllowRememberConsent = true,
AllowOfflineAccess = true,
RedirectUris = { "https://localhost:5002/signin-oidc"},
PostLogoutRedirectUris = { "https://localhost:5002/signout-callback-oidc"},
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.Phone,
IdentityServerConstants.StandardScopes.Address,
"example.com.identity"
}
}
客户端运行有意登录(用户单击“登录”链接),如下所示:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Login()
{
await HttpContext.SignOutAsync("oidc");
await HttpContext.ChallengeAsync("oidc",
new AuthenticationProperties() {
RedirectUri = Url.Action("LoginCallback")
});
}