无法使用Owin的Asp.Net标识SignOut

时间:2015-06-27 02:02:19

标签: asp.net-mvc-5 owin asp.net-identity-2

我无法使用带有Owin的ASP.NET身份(最新版本)在MVC 5应用程序中注销用户。登录效果很好......但我无法在不打开浏览器设置的情况下关闭用户以删除cookie。

当LogOff操作运行时,浏览器会重定向到具有[Authorize]属性的指定页面。它应该在那时被拒绝并重定向到登录页面。

请注意,如果我手动删除Cookie,它会在尝试打开[授权]页面时正确重定向,因此未经身份验证的用户的重定向操作正常运行。

我看到很多类似的问题,并尝试过这些解决方案,但到目前为止还没有任何工作。

我改变了:

AuthenticationManager.SignOut();

要:

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

正如之前的答案中所建议的那样,但它没有改变行为。

登录正常。我注意到在尝试LogOff之后,有两个具有相同名称的cookie而不是一个。一个cookie是空的,一个不是。

这是我的LogOff方法:

[HttpPost]
    [AllowAnonymous]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

        //Clear the principal to ensure the user does not retain any authentication
        HttpContext.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);

        // Redirect to a controller/action that requires authentication to ensure a redirect takes place
        // this clears the Request.IsAuthenticated flag since this triggers a new request
        return RedirectToLocal(String.Empty);
    }

我的OwinStartup课程:

public class OwinStartup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new TenantDbContext()));

        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(TenantDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            CookieSecure = CookieSecureOption.Always,
            Provider = new CookieAuthProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
    }

    public class CookieAuthProvider : CookieAuthenticationProvider
    {
        public override void ResponseSignIn(CookieResponseSignInContext context)
        {
            context.CookieOptions.Domain = context.Request.Uri.Host;   
            base.ResponseSignIn(context);
        }
    }
}

这是我的AuthenticationManager:

private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

0 个答案:

没有答案