2017-07-27 51 views
0

我使用以下代码更新我的Web应用程序中用户的声明。但是,要更新此用户的声明/ Cookie,我想强制他再次登录。所以基本上我想在更新索赔后过期他的cookie。任何想法如何做到这一点?更新远程用户声明

await _signInManager.RefreshSignInAsync(user);是我尝试的第一件事,但因为我正在更新另一个用户的声明而失败:)

我发现的所有其他示例都与RefreshSignInAsync差不多,并且不处理我正在更新的事实另一个用户的声明。

public async Task<IActionResult> AddClaimPost(string id) 
     { 
      var user = _context.ApplicationUser 
       .SingleOrDefault(m => m.Id == id); 


      foreach(var item in Request.Form) 
      { 
       if (item.Key.Contains("Claim")) 
       { 
        if (item.Value.Contains("true")) 
        { 
         if (!User.HasClaim(item.Key, item.Key)) 
         { 
          var result = await _userManager.AddClaimAsync(user, new Claim(item.Key, item.Key)); 
         } 
        } 
        else 
        { 
         var result2 = await _userManager.RemoveClaimAsync(user, new Claim(item.Key, item.Key)); 
        } 
       } 
      } 

      await _signInManager.RefreshSignInAsync(user); 

      return RedirectToAction("Overview"); 
     } 

回答

0

搜索了几天后,我发现我想要的是不可能的。你不能逼出来注销用户,而无需把饼干时间跨度为0

options.Cookies.ApplicationCookie.ExpireTimeSpan = 0;

在这种情况下,它会在每次用户发出请求时检查Cookie。用以下代码可以强制用户再次登录:

await _userManager.UpdateSecurityStampAsync(user);