0

如何限制與交付式在特定的SPA客戶機的每個客戶端應用程序登錄的x量 - 隱式隱許可SPA

This is out of scope within Identity server

解試圖 -

  1. 訪問令牌持久存儲到數據庫,但是這種方法客戶端不更新訪問令牌而沒有進入代碼,因爲客戶端瀏覽器請求帶有有效令牌,儘管它已過期無提示認證正在通過更新令牌發出一個新的參考標記(可以在表persistgrants token_type'reference_token'中看到)

  2. Cookie事件 - 關於validateAsync - 雖然這隻適用於服務器web,但我們不能將這個邏輯放在SPA客戶端的oidc庫。

  3. 自定義signInManager通過重寫SignInAsync - 但執行沒有到達調試模式的這一點,因爲IDM一直認識到用戶有一個有效的托克(雖然已過期)不斷髮出令牌(請注意,沒有刷新令牌這裏通過存儲和修改管理它!!!)

任何線索如何IDM重的問題沒有考慮用戶的登錄屏幕的道理,即使訪問令牌過期??(Silent authentication? ?

回答

0

實現配置文件服務覆蓋activeasync

public override async Task IsActiveAsync(IsActiveContext context) 
    { 
     var sub = context.Subject.GetSubjectId(); 
     var user = await userManager.FindByIdAsync(sub); 

     //Check existing sessions 
     if (context.Caller.Equals("AccessTokenValidation", StringComparison.OrdinalIgnoreCase)) 
     { 
      if (user != null) 
       context.IsActive = !appuser.VerifyRenewToken(sub, context.Client.ClientId); 
      else 
       context.IsActive = false; 
     } 
     else 
      context.IsActive = user != null; 
    } 

啓動

services.AddTransient<IProfileService, ProfileService>(); 

,同時在配置服務

.AddProfileService<ProfileService>(); 

更新

Session.Abandon(); //is only in aspnet prior versions not in core 
Session.Clear();//clears the session doesn't mean that session expired this should be controlled by addSession life time when including service. 

我添加身份服務器的服務集合碰巧找到了一種更好的方法,即使用aspnetuser securitystamp,每次用戶登錄時都要更新安全標記,以便任何之前的活動會話/ cookie都會失效。

_userManager.UpdateSecurityStampAsync(_userManager.FindByEmailAsync(model.Email).Result).Result 

更新(最終):

在登入: -

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, false); 
       if (result.Succeeded) 
       { 
      //Update security stamp to invalidate existing sessions   
        var user = _userManager.FindByEmailAsync(model.Email).Result; 
        var test= _userManager.UpdateSecurityStampAsync(user).Result; 
        //Refresh the cookie to update securitystamp on authenticationmanager responsegrant to the current request 
        await _signInManager.RefreshSignInAsync(user); 
      } 

檔案服務實現: -

public class ProfileService : ProfileService<ApplicationUser> 

{ 
public override async Task IsActiveAsync(IsActiveContext context) 
     { 
      if (context == null) throw new ArgumentNullException(nameof(context)); 
      if (context.Subject == null) throw new ArgumentNullException(nameof(context.Subject)); 

      context.IsActive = false; 

      var subject = context.Subject; 
      var user = await userManager.FindByIdAsync(context.Subject.GetSubjectId()); 

      if (user != null) 
      { 
       var security_stamp_changed = false; 

       if (userManager.SupportsUserSecurityStamp) 
       { 
        var security_stamp = (
         from claim in subject.Claims 
         where claim.Type =="AspNet.Identity.SecurityStamp" 
         select claim.Value 
         ).SingleOrDefault(); 

        if (security_stamp != null) 
        { 
         var latest_security_stamp = await userManager.GetSecurityStampAsync(user); 
         security_stamp_changed = security_stamp != latest_security_stamp; 
        } 
       } 

       context.IsActive = 
        !security_stamp_changed && 
        !await userManager.IsLockedOutAsync(user); 
      } 
     } 
    } 

*

掛鉤的服務集合中: -

*

services.AddIdentityServer() 
    .AddAspNetIdentity<ApplicationUser>()     
     .AddProfileService<ProfileService>(); 

即在每次登錄時,用戶的安全戳更新,並且推送到cookie,當令牌到期時,授權端點將會驗證安全性變化,如果有,則重定向用戶登錄。這樣我們確保只有一個活動會話