【发布时间】:2016-04-16 12:03:46
【问题描述】:
我在外部身份验证服务器中使用 OWIN 中间件,我的应用程序使用 OAuth 授权代码授予流程对其进行身份验证。
我可以重定向到身份验证服务器,针对外部提供商 (Google) 进行身份验证,并使用已登录的用户和应用程序 Cookie 设置重定向回我的客户端应用程序,但是当我尝试注销时,cookie 在我之后仍然存在调用AuthenticationManager.SignOut 方法。
Startup.Auth.cs 中我的 cookie 选项是:
var cookieOptions = new CookieAuthenticationOptions
{
Provider = cookieProvider,
AuthenticationType = "Application",
AuthenticationMode = AuthenticationMode.Passive,
LoginPath = new PathString("/Account/Index"),
LogoutPath = new PathString("/Account/Logout"),
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromMinutes(30),
};
app.UseCookieAuthentication(cookieOptions);
app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
我的登录方式:
var loginInfo = await AuthManager.GetExternalLoginInfoAsync();
SignInManager.ExternalSignInAsync(loginInfo, true);
var identity = AuthManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie).Result.Identity;
if (identity != null)
{
AuthManager.SignIn(
new AuthenticationProperties {IsPersistent = true},
new ClaimsIdentity(identity.Claims, "Application", identity.NameClaimType, identity.RoleClaimType));
var ticket = AuthManager.AuthenticateAsync("Application").Result;
var identity = ticket != null ? ticket.Identity : null;
if (identity == null)
{
AuthManager.Challenge("Application");
return new HttpUnauthorizedResult();
}
identity = new ClaimsIdentity(identity.Claims, "Bearer", identity.NameClaimType, identity.RoleClaimType);
AuthManager.SignIn(identity);
}
return Redirect(Request.QueryString["ReturnUrl"]);
退出方式:
var authTypeNames = new List<string>();
authTypeNames.Add("Google");
authTypeNames.Add("Application");
authTypeNames.Add("Bearer");
authTypeNames.Add(DefaultAuthenticationTypes.ExternalCookie);
Request.GetOwinContext().Authentication.SignOut(authTypeNames.ToArray());
我查看了其他问题,例如: OWIN authentication, expire current token and remove cookie 和 OWIN - Authentication.SignOut() doesn't remove cookies
没有运气。我知道我可以通过设置负的到期日期来手动删除 cookie,但如果可能的话,我更愿意使用内置方法。
如何在我退出时删除应用程序 Cookie?
【问题讨论】:
-
好吧.. 这不是第一次被指出stackoverflow.com/questions/22571696/…
-
@ymz,这是一个不同的问题。我问的是使用从另一个应用程序调用的外部身份验证服务器注销。
标签: c# authentication cookies asp.net-identity owin