如何删除Owin Token?

时间:2016-02-29 11:46:31

标签: c# asp.net-mvc asp.net-web-api owin

我尝试通过SignOut删除,但不会删除或过期。

var authentication = HttpContext.Current.GetOwinContext().Authentication;

authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie,
                              Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie,
                              Microsoft.AspNet.Identity.DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie,
                              Microsoft.AspNet.Identity.DefaultAuthenticationTypes.TwoFactorCookie);

        authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
        authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
        authentication.SignOut();

我试图在很多博客(msdn,堆栈溢出等)中找到解决方案,但还没有看到解决方案。

我通过以下方式生成令牌:

// Set claim
var identityType = new GenericIdentity(userName, Config.Constants.BASICIDENTITYTYPE);
string[] roles = null;
var principal = new GenericPrincipal(identityType, roles);
Thread.CurrentPrincipal = principal;

if (HttpContext.Current != null)
{
    HttpContext.Current.User = principal;
}

var identity = new ClaimsIdentity(StartUp.OAuthBearerOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
identity.AddClaim(new Claim(Config.Constants.USERNAME, userName));
identity.AddClaim(new Claim(Config.Constants.USERID, userId.ToString()))

// Generate ticket
AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());

var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;

TimeSpan expiryTime = Constants.TOKEN_EXPIRY_TIME_SECOND);
ticket.Properties.ExpiresUtc = currentUtc.Add(expiryTime);

return StartUp.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);

1 个答案:

答案 0 :(得分:0)

在最简单的身份配置中,尝试:

Request.GetOwinContext().Authentication.SignOut();

它应该完成这项工作

希望这有帮助。