【问题标题】:How to logout all users in ASP.NET Core cookie authentication?如何注销 ASP.NET Core cookie 身份验证中的所有用户?
【发布时间】:2017-05-17 00:31:29
【问题描述】:

我正在使用带有 CookieAuthentication 的 ASP.NET Core MVC。有没有办法让所有用户同时退出?我尝试重置 IIS - 没有用。我尝试删除所有用户的会话(我正在使用数据库进行会话存储) - 没有用。

有什么想法吗?

【问题讨论】:

  • 要注销所有用户,可以更改加密密钥或重命名身份验证cookie。
  • @Zygimantas 检查这个答案。 stackoverflow.com/a/36050939/5233410
  • @Nkosi:您的链接中使用了身份服务器。问题没有提到它,只是简单的 cookie 身份验证。

标签: asp.net asp.net-core asp.net-core-mvc


【解决方案1】:

您可以使用CookieAuthenticationOptions.SessionStore属性,将身份信息存储在服务器端,以便您在需要时将其清除。

public void ConfigureServices(IServiceCollection services)
{
    MemoryCacheTicketStore memoryCacheTicketStore = new MemoryCacheTicketStore();
    services.AddSingleton<MemoryCacheTicketStore>(memoryCacheTicketStore);

    services.AddAuthentication().AddCookie(cfg =>
    {
        cfg.SessionStore = memoryCacheTicketStore;
    });
}

public class SessionController : Controller
{
    private readonly MemoryCacheTicketStore memoryCacheTicketStore;

    public SessionController(MemoryCacheTicketStore memoryCacheTicketStore)
    {
        this.memoryCacheTicketStore = memoryCacheTicketStore;
    }

    public Task ClearAllSession()
    {
        return memoryCacheTicketStore.ClearAll();
    }
}

public class MemoryCacheTicketStore : ITicketStore
{
    private const string KeyPrefix = "AuthSessionStore-";
    private IMemoryCache _cache;

    public MemoryCacheTicketStore()
    {
        _cache = new MemoryCache(new MemoryCacheOptions());
    }

    public async Task ClearAll()
    {
        _cache.Dispose();
        _cache = new MemoryCache(new MemoryCacheOptions());
    }

    public async Task<string> StoreAsync(AuthenticationTicket ticket)
    {
        var guid = Guid.NewGuid();
        var key = KeyPrefix + guid.ToString();
        await RenewAsync(key, ticket);
        return key;
    }

    public Task RenewAsync(string key, AuthenticationTicket ticket)
    {
        var options = new MemoryCacheEntryOptions();
        var expiresUtc = ticket.Properties.ExpiresUtc;
        if (expiresUtc.HasValue)
        {
            options.SetAbsoluteExpiration(expiresUtc.Value);
        }
        options.SetSlidingExpiration(TimeSpan.FromHours(1)); // TODO: configurable.

        _cache.Set(key, ticket, options);

        return Task.FromResult(0);
    }

    public Task<AuthenticationTicket> RetrieveAsync(string key)
    {
        AuthenticationTicket ticket;
        _cache.TryGetValue(key, out ticket);
        return Task.FromResult(ticket);
    }

    public Task RemoveAsync(string key)
    {
        _cache.Remove(key);
        return Task.FromResult(0);
    }
}

【讨论】:

    【解决方案2】:

    使用 CookieAuthentication,cookie 只是一个包含用户名、角色和辅助数据的加密字符串。简而言之,它识别的是用户,而不是会话。杀死会话不会使cookie失效。

    话虽如此,您可以在 cookie 的辅助数据中填充会话标识符或其他令牌,然后在身份验证过程中对其进行验证。可以在here 找到有人尝试这样做的示例。

    另一个选项是您可以暂时禁用用户存储库中的用户,而不是使会话无效。 Here 是使用 ASPNET Identity 2.0 的示例。

    第三个(核)选项是更改所有 Web 服务器上的 machine key,这将使任何旧表单身份验证 cookie 无法读取,从而迫使所有用户重新登录。

    【讨论】:

      【解决方案3】:

      这很简单。 更改登录 cookie 名称

      在 startup.cs 中,将默认名称更改为任何名称。

       options.Cookie.Name = "NewName";
      

      完整示例:

        services.ConfigureApplicationCookie(options =>
                  {
                      options.Cookie.Name = "NewName"; //<-- Here
                      options.Cookie.HttpOnly = true;
                    ...
                      options.Events = options.Events ?? new CookieAuthenticationEvents();
                      var onForbidden = options.Events.OnRedirectToAccessDenied;
                      var onUnauthorized = options.Events.OnRedirectToLogin;
                      options.Events.OnRedirectToAccessDenied = (context) => OnRedirect(context, onForbidden, HttpStatusCode.Forbidden);
                      options.Events.OnRedirectToLogin = (context) => OnRedirect(context, onUnauthorized, HttpStatusCode.Unauthorized);
                  });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-03
        • 2017-03-30
        • 2021-03-29
        • 2010-11-07
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        • 2018-09-02
        相关资源
        最近更新 更多