我在我的应用程序中使用asp.net Identity 2.0进行身份验证(Owin中间件)。
session 劫持:
当我登录时,Identity创建AspNet.ApplicationCookie.then,然后我复制了AspNet.ApplicationCookie值。然后我从应用程序中注销。注销后,我正在手动创建cookie(AspNet.ApplicationCookie)并进行刷新,它将重定向我的主页。

特权提升:
同时我以用户AI复制(AspNet.ApplicationCookie)的cookie登录并退出了我。以用户BI的身份登录后,我正在编辑用户B Cookie,然后粘贴用户A cookie并保存。刷新后在浏览器中,我可以获得UserA的访问和身份验证。

我注销所有 session 并删除所有cookie。即使Asp.Net Identity(Owin)每次都会生成新的AspNet.ApplicationCookie。但是它仍然接受旧的cookie并给我访问权限。不知道为什么吗?
退出后,任何人都可以给我如何使旧的AspNet.ApplicationCookie无效的信息。
这是我在Startup.Auth.cs中的代码

 public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);


    }

//这是注销代码
    public ActionResult LogOff ( )
    {
        //Delete all cookies while user log out
        string[] myCookies = Request.Cookies.AllKeys;
        foreach ( var cookies in myCookies )
        {
            Response.Cookies[ cookies ].Expires = DateTime.Now.AddDays(-1);

        }
        Request.GetOwinContext( ).Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

        // AuthenticationManager.SignOut( );
        Session.Clear( );
        Session.RemoveAll( );
        Session.Abandon( );
        return RedirectToAction("LoginPage", "Account");
    }

//这是我的登录 Controller 代码
 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindAsync(model.UserName, model.Password);
            if (user != null)
            {
                await SignInAsync(user, model.RememberMe);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

最佳答案

这是设计使然。允许您从多个浏览器登录并仅在单击“注销”的浏览器中注销,而不能在所有其他浏览器中注销。

但是在注销时,您可以在用户上更新SecurityStamp,然后在很短的时间内设置安全标记验证期。

这将更改安全戳:

await userManager.UpdateSecurityStampAsync(user.Id);

将其放入您的注销方法中。

然后以这种方式在Startup.Auth.cs中修改UseCookieAuthentication:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login")
    Provider = new CookieAuthenticationProvider
    {
        // 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(1), // set this low enough to optimise between speed and DB performance
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
    }
});

这种方法的唯一缺点是-不执行注销过程时-什么也不会发生。当注销时,它将注销所有其他 session 。

08-04 09:10