我正在使用IdentityServer 3进行身份验证。我有2个客户端应用程序,一种是使用经典ASP.NET MVC 5和另一个使用ASP.NET Core开发的。这两个应用程序均已注销功能如下:
经典ASP.NET MVC 5
应用程序启动
public class Startup
{
public void Configuration(IAppBuilder app)
{
var CK = new CookieAuthenticationOptions()
{
AuthenticationType = "Cookies",
CookieName = "MyCookie"
};
app.UseCookieAuthentication(CK);
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://login.mydomain.com/identity",
Scope = "openid profile",
ClientId = "myclientid",
RedirectUri = "http://localhost:34937/",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies",
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = (context) =>
{
// do claim transformation here
},
RedirectToIdentityProvider = (n) =>
{
if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
{
var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
n.ProtocolMessage.IdTokenHint = idTokenHint;
}
return Task.FromResult(0);
}
}
}
}
帐户控制器具有注销操作
public class AccountController:Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
Request.GetOwinContext().Authentication.SignOut();
return Redirect("/");
}
}
asp.net core
应用程序启动
public static class IApplicationBuilderExtensions
{
public static void UseIdentityServer(this IApplicationBuilder app, string authority, string clientId)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
LoginPath = "/home",
AccessDeniedPath = new PathString(IdentityConstant.AccessDeniedPath),
CookieName = "MtAuthCookie",
SlidingExpiration = true
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
var connectOptions = new OpenIdConnectOptions()
{
AutomaticChallenge = true,
Authority = authority,
ClientId = clientId,
ResponseType = "id_token",
AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme,
SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme,
CallbackPath = "/home",
Events = new OpenIdConnectEvents()
{
OnTokenValidated = async context =>
{
//create new identity to store only required claims here.
},
OnRedirectToIdentityProvider = async context =>
{
if (context.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
{
var idTokenHint = context.HttpContext.User.FindFirst("id_token");
if (idTokenHint != null)
context.ProtocolMessage.IdTokenHint = idTokenHint.Value;
}
await Task.FromResult(0);
}
}
};
app.UseOpenIdConnectAuthentication(connectOptions);
}
}
}
帐户控制器具有注销操作
public class AccountController:Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LogOff()
{
if (User.Identity.IsAuthenticated)
{
await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
return Redirect("/");
}
}
问题
在经典的ASP.NET记录中,动作正常。我看到它执行OnRedirectToIdentityProvider
事件,并且context.ProtocolMessage.RequestType
设置为LogoutRequest
,此后它使GET
请求为:
https://login.mydomain.com/indentity/connect/endsessioncallback?sid=xxxxxx
最终在https://devlogin.crowdreason.com/indentity/logout中登录的用户登录
但是,在ASP.NET中,https://login.mydomain.com/indesity/connect/endsession从未在注销行动时被调用。我还注意到context.ProtocolMessage.RequestType
永远不会设置为Logout
。登录用户实际上会自动验证并返回家庭页面?
也
我在ASP.NET Core中缺少什么?是否有IdentityServer3
和ASP.NET Core
客户端可用的样本?(注意我不使用IdentityServer4)
我认为有一个不同的事件。这对我有用:
OnRedirectToIdentityProviderForSignOut = context =>
{
var idTokenHint = context.HttpContext.User.FindFirst("id_token");
if (idTokenHint != null)
{
context.ProtocolMessage.IdTokenHint = idTokenHint.Value;
}
return Task.FromResult(0);
}