我关注this tutorial但它没有告诉你如何退出。我试着这样做
Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
Request.GetOwinContext().Authentication.SignOut()
Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
你可以在这里获得示例代码:https://github.com/AndersAbel/SocialLoginWithoutIdentity
只需要再添加一个动作
public ActionResult SignOut()
{
Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
return RedirectToAction("Index", "Home");
}
这个方法加上我在上面发布的3行中的任何一行
我现在的结果是,我登录后,我转到安全页面,可以看到它,然后我继续我的注销,然后在注销后尝试返回安全页面,我被允许回到那个安全页面。
所以它实际上并没有真正让我退出。
如本教程中所述,使用的中间件使用默认的身份验证类型,但不要覆盖它。
通过仅使用externalCookie作为Owin的参数,您正在清除Asp的cookie,但不是用于存储Google提供程序的cookie,
为此,您必须获取所有当前cookie的数组。它可以通过这样简单的方式完成:
Request.GetOwinContext()
.Authentication
.SignOut(HttpContext.GetOwinContext()
.Authentication.GetAuthenticationTypes()
.Select(o => o.AuthenticationType).ToArray());
这就是在Tutorial上说的地方:
对于UseGoogleAuthentication的调用应该非常明显,为什么需要它。
但第一个toSetDefaultSignInAsAuthenticationType并不那么明显。登录中间件通常依赖于社交登录中间件之前注册的外部cookie中间件。外部cookie中间件,它将自己设置为默认的登录类型。这就是社交登录中间件知道应该使用外部cookie的方式。在此设置中没有外部cookie,因此我们必须手动将主cookie中间件设置为默认的登录类型。如果AuthenticationType与社交登录中间件创建的身份中的一个匹配,则cookie中间件将仅发出cookie。在owin外部认证管道中查找社交的设置
尝试设置缓存控制标头。
public ActionResult SignOut() {
var authenticationTypes = new string[] {
DefaultAuthenticationTypes.ApplicationCookie,
DefaultAuthenticationTypes.ExternalCookie
};
AuthenticationManager.SignOut(authenticationTypes);
// HACK: Prevent user from being able to go back to a logged in page once logged out
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
// now redirect
return RedirectToAction("Index", "Home");
}
private IAuthenticationManager AuthenticationManager {
get {
return Request.GetOwinContext().Authentication;
}
}
除非您尝试使用可以禁用的JavaScript,否则无法停止用户单击浏览器上的后退按钮。用户可以返回页面并查看上一页上的内容,但如果他们尝试单击任何受保护的链接或刷新页面,他们将被重定向以登录。
在需要授权的类上使用[Authorize]属性:
[Authorize]
public class MeController : ApiController
{
// GET api/<controller>
public IEnumerable<object> Get()
{
var identity = User.Identity as ClaimsIdentity;
return identity.Claims.Select(c => new
{
Type = c.Type,
Value = c.Value
});
}
}
来源:http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server