![](/img/trans.png)
[英]IdentityServer4 .net core 2 Authentication Use multiple instances of OpenIDConnect
[英]Cannot sign out the OpenIdConnect authentication of identityserver4 on ASP.NET Core 2 application
我的身份服务器使用的是 identityserver4 框架( http://localhost:9000 )。 我在身份服务器上注册客户端,如下所示。
clients.Add(
new Client
{
ClientId = "customer.api",
ClientName = "Customer services",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
RequireConsent = false,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:60001/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:60001/signout-callback-oidc" },
ClientSecrets = new List<Secret>
{
new Secret("testsecret".Sha256())
},
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess,
"customerprivatelinesvn.api",
},
AllowOfflineAccess = true,
AlwaysIncludeUserClaimsInIdToken = true,
AllowedCorsOrigins = { "http://localhost:60001" }
});
这是我的客户端应用程序 ( http://localhost:60001 ) 上的身份验证。
private void AddAuthentication(IServiceCollection services)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie()
.AddOpenIdConnect("oidc", options =>
{
Configuration.GetSection("OpenIdConnect").Bind(options);
});
}
"OpenIdConnect": {
"SignInScheme": "Cookies",
"Authority": "http://localhost:9000/",
"RequireHttpsMetadata": false,
"ClientId": "customer.api",
"ClientSecret": "testsecret",
"Scope": [ "customerprivatelinesvn.api", "offline_access" ],
"CallbackPath": "/signin-oidc",
"ResponseType": "code id_token token",
"GetClaimsFromUserInfoEndpoint": true,
"SaveTokens": true
}
客户端应用程序的 HomeController
[Authorize]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
我尝试执行如下注销操作
public class AccountController : Controller
{
public async Task<IActionResult> Signout()
{
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
return RedirectToAction("Index", "Home");
}
}
但是当用户退出时,它不会调用身份服务器的 endsession 端点。 我看fiddler的流量,没有对身份服务器的请求。
我的期望是当用户退出时,它会调用身份服务器的 endsession 端点并重定向到身份服务器的注销链接,如下所示。
我们可以通过调用 OwinContext signout 在 MVC 应用程序上轻松完成此操作
private void LogoutOwin(IOwinContext context)
{
context.Authentication.SignOut();
}
但是注销方法在 ASP.NET Core 2 上不再起作用。
注意:我正在从 AJAX 帖子调用注销操作,因为我的客户端应用程序是 angular 5 应用程序。
有谁知道如何在 ASP.NET Core 2 上正确实现注销?
非常感谢。
问候,
凯文
我现在可以解决我的问题。
1) 返回 SignOutResult 将调用 endsession 端点。
2) 更改 AJAX 帖子以提交表单。
public class AccountController : Controller
{
public IActionResult Signout()
{
return new SignOutResult(new[] { "oidc", "Cookies" });
}
}
<form action="/Account/Signout" id="signoutForm" method="post" novalidate="novalidate">
<ul class="nav navbar-nav navbar-right">
<li><a href="javascript:document.getElementById('signoutForm').submit()">Sign out</a></li>
</ul>
</form>
要允许注销,请使用以下注销操作:
public async Task Logout()
{
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
}
这正是快速入门所说的使用( http://docs.identityserver.io/en/release/quickstarts/3_interactive_login.html )。 你(和我)太聪明了。 我查看了教程中的操作并认为“这还没有完成,它没有返回操作结果,让我们重定向回我的页面”。
实际上发生的是HttpContext.SignOutAsync("oidc");
设置默认 ActionResult(即重定向到 OpenIdConnect 提供程序以完成注销)。 通过指定你自己的return RedirectToAction("Index", "Home");
你覆盖它,所以注销操作永远不会发生。
根据此答案,您在注销完成后指定重定向 URL 的方式是使用 AuthenticationProperties
public async Task Logout()
{
await context.SignOutAsync("Cookies");
var prop = new AuthenticationProperties
{
RedirectUri = "/logout-complete"
};
// after signout this will redirect to your provided target
await context.SignOutAsync("oidc", prop);
}
在 Net Core 2.0 上更改您的代码以使用枚举 CookieAuthenticationDefaults 和 OpenIdConnectDefaults
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(SetOpenIdConnectOptions);
private static void SetOpenIdConnectOptions(OpenIdConnectOptions options)
{
options.ClientId = "auAuthApp_implicit";
options.Authority = "http://localhost:55379/";
options.SignInScheme = "Cookies";
options.RequireHttpsMetadata = false;
options.SaveTokens = true;
options.ResponseType = "id_token token";
options.GetClaimsFromUserInfoEndpoint = true;
}
和...
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
return RedirectToAction("Index", "Home");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.