2016-03-22 7 views
5

私がしたいのは、一度に1つのデバイスにしかログインできないようにユーザーIDを制限することです。たとえば、ユーザーID「abc」は自分のコンピュータにログインします。ユーザID「abc」は現在、自分の電話機からログインしようとします。私がしたいのは、自分のコンピュータ上でセッションを終了させることです。Asp.net mvc identity SecurityStamp signout everywhere

私はAsp.net mvc identityのメンバーシップを使用しており、この目的でSecurityStampを使用しています。これは、アカウント/ログインアクションで私のコードです:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     var user = UserManager.FindByEmail(model.Email); 
     var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false); 
     await UserManager.UpdateSecurityStampAsync(user.Id); 

UpdateSecurityStampAsync方法ドキュメントによると、言う:SignOutEverywhere機能のために使用するユーザーのための新しいセキュリティスタンプを生成します。しかし、それは動作しません。

答えて

3

他のデバイスでCookieを即座に無効にするには、Cookieを検証するためにすべてのリクエストでデータベースにアクセスする必要があります。これを行うには、あなたはAuth.Config.csでクッキーの無効化を設定し、0にvalidateIntervalを設定する必要があります。

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    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<UserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromSeconds(0), 
        regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
    }    
); 
+0

はい。毎回のリクエストでデータベースが追加されます – tmg

+0

PasswordSignInAsyncの前にUpdateSecurityStampAsyncを試してください – tmg

+0

ログアウトできない、またはログアウトしていない他のデバイスにログアウトできますか? – tmg

関連する問題