2017-10-05 52 views
0

我認爲問題標題不言自明,也許只是一個精確的時候,當我說「他或她正在瀏覽」時,我正在考慮傳播或信號。如何讓任何視圖知道用戶角色(ASP.NET MVC身份)已更改,以強制任何用戶在他或她瀏覽時註銷?

我不希望他或她必須瀏覽另一個地方,只是想知道身份SecurityStamp已更改並已登出然後重定向到主頁,我已經這樣做了,但我已經想知道是否有框架(我懷疑最有可能是JS)會使操作更「實時」一點。

[編輯]
可能是SignalR的工作,我還沒有嘗試過。

+0

是的,SignalR將是更好的選擇。 – DSR

+0

不確定你的負載需求,或者你想如何「實時」,但是一個簡單的調查也可以工作 – Shoe

回答

0

我得到了一個有效的解決方案與SignalR

首先,支付順序注意其中SignalR是在Startup.Auth.csHow to send message via SignalR to a specific User(Identity Id)?設置,也創造IUserIdProvider的實現,將是registred後,才Cookies和OwinContext,以使其能夠利用身份用戶字段(即非空)。

public partial class Startup 
{ 
    public void ConfigureAuth(IAppBuilder appBuilder) 
    { 
     // Order matters here... 
     // Otherwise SignalR won't get Identity User information passed to Id Provider... 
     ConfigOwinContext(appBuilder); 
     ConfigCookies(appBuilder); 
     ConfigSignalR(appBuilder); 
    } 

    private static void ConfigOwinContext(IAppBuilder appBuilder) 
    { 
     appBuilder.CreatePerOwinContext(ApplicationDbContext.Create); 
     appBuilder.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     appBuilder.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 
     appBuilder.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); 
     appBuilder.CreatePerOwinContext(LdapAdEmailAuthenticator.Create); 
    } 

    private static void ConfigCookies(IAppBuilder appBuilder) 
    { 
     appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser> 
       (
        TimeSpan.FromHours(4), 
        (manager, user) => user.GenerateUserIdentityAsync(manager) 
       ) 
      } 
     }); 
     appBuilder.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 
     appBuilder.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5)); 
     appBuilder.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie); 
    } 

    private static void ConfigSignalR(IAppBuilder appBuilder) 
    { 
     appBuilder.MapSignalR(); 
     var idProvider = new HubIdentityUserIdProvider(); 
     GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider),() => idProvider); 
    } 
} 

public class HubIdentityUserIdProvider : IUserIdProvider 
{ 
    public string GetUserId(IRequest request) 
    { 
     return request == null 
      ? throw new ArgumentNullException(nameof(request)) 
      : request.User?.Identity?.GetUserId(); 
    } 
} 

其次,聲明一個輪轂上的服務器側

public class UserHub : Hub 
{ 
} 

第三,在控制器(API或不),其中的變化,其涉及特定用戶的一個註銷,強制signout +身份securitystamp的更新:

var userHub = GlobalHost.ConnectionManager.GetHubContext<UserHub>(); 
userHub.Clients.User(userId).send("Roles added: " + rolesToAdd.Join() + Environment.NewLine + "Roles removed: " + rolesToRemove.Join()); 

return Request.CreateResponse(HttpStatusCode.OK); 

第四,使用輪轂上JS客戶端,我創建LoggedOutPartialView.cshtml噹噹前用戶被認證其僅使用的,局部視圖:

@if (Request.IsAuthenticated) 
{ 
    <div class="modal fade" id="loggedOutModal" tabindex="-1" role="dialog" aria-labelledby="loggedOutModalLabel"> 
     <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
        <h4 class="modal-title" id="loggedOutModalLabel">Notification</h4> 
       </div> 
       <div class="modal-body"> 
        <h6 class="align-center">Sorry, but it seems that you just have been logged out!!!</h6> 
       </div> 
       <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 
      </div> 
     </div> 
    </div> 

    <script> 

     $(function() { 
      var userHub = $.connection.userHub; 

      console.log(userHub.client); 

      userHub.client.logout= function (message) { 
       $('#loggedOutModal').modal('show'); 
      }; 

      $.connection.hub.start().done(function() { 
      }); 
     }); 

    </script> 
} 
相關問題