web-dev-qa-db-ja.com

ASP.NET Identity 2.0.1を使用して、役割の変更をユーザーに強制的に伝達するにはどうすればよいですか?

this を読みました。しばらくすると、ロールの変更が最終的にユーザーCookieにどのように伝播するかを説明していますが、を強制する方法がわかりません。即時ユーザーロールへの変更。

管理者としての役割を変更するときに、本当にユーザーをサインアウトする必要がありますか?もしそうなら—どのように? AuthenticationManager.SignOut();を使用する場合は、役割を変更したいユーザーではなく、自分自身(admin)をサインオフします。

現在、await UserManager.UpdateSecurityStampAsync(user.Id);を使用して新しいセキュリティスタンプを生成していますが、機能しません。別のユーザーとしてログインしているときに別のブラウザーでページを更新しても、彼の主張(セキュリティスタンプを含む)は変更されません。

27
Intoccabil

Cookieの即時失効を有効にする場合は、すべてのリクエストがデータベースにヒットしてCookieを検証する必要があります。したがって、遅延間のトレードオフはデータベースの負荷にあります。ただし、validationIntervalはいつでも0に設定できます。

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.FromSeconds(0),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});
19
Hao Kung