2017-05-16 17 views
0

ユーザーがログインした後でweb apiでユーザークレームを更新する必要がありますが、ユーザークレームを更新した後も以前の値が返されます。 蛇腹コードは、ユーザがログインした後にアクティブユーザグループを更新するために使用される。ユーザー権利の更新はASP.NET IDに影響しませんか?

/// <summary> 
/// The class AppUser 
/// </summary> 
public class AppUser : ClaimsPrincipal 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="AppUser"/> class. 
    /// </summary> 
    /// <param name="principal">The principal.</param> 
    public AppUser(ClaimsPrincipal principal) 
     : base(principal) 
    { 
    } 

    /// <summary> 
    /// Gets the name. 
    /// </summary> 
    /// <value> 
    /// The name. 
    /// </value> 
    public string Name 
    { 
     get 
     { 
      return this.FindFirst(ClaimTypes.Name).Value; 
     } 
    } 

    /// <summary> 
    /// Gets the name of the user. 
    /// </summary> 
    /// <value> 
    /// The name of the user. 
    /// </value> 
    public string UserName 
    { 
     get 
     { 
      return this.FindFirst("UserName").Value; 
     } 
    } 

    /// <summary> 
    /// Gets the active group. 
    /// </summary> 
    /// <value> 
    /// The active group. 
    /// </value> 
    public string ActiveGroup 
    { 
     get 
     { 
      return ((ClaimsIdentity)this.Identity).FindFirst("ActiveGroup").Value; 
     } 
    } 

    /// <summary> 
    /// Gets the email. 
    /// </summary> 
    /// <value> 
    /// The email. 
    /// </value> 
    public string Email 
    { 
     get 
     { 
      return this.FindFirst("Email").Value; 
     } 
    } 
} 


/// <summary> 
/// The class BaseController 
/// </summary> 
public class BaseController : ApiController 
{ 
    /// <summary> 
    /// Gets the current user. 
    /// </summary> 
    /// <value> 
    /// The current user. 
    /// </value> 
    public AppUser CurrentUser 
    { 
     get 
     { 
      return new AppUser(this.User as ClaimsPrincipal); 
     } 
    } 
} 



public class AccountController : BaseController 
{ 

    [HttpPost] 
    [Route("UpdateUserGroup")] 
    public int UpdateUserGroup(string userGroup) 
    { 
     var user = User as ClaimsPrincipal; 
     var identity = user.Identity as ClaimsIdentity; 
     identity.RemoveClaim(identity.FindFirst("ActiveGroup")); 
     identity.AddClaim(new Claim("ActiveGroup", this.GetRoleNameByPresenter(userGroup))); 
     return 1; 
    } 
} 

答えて

1

問題の特許請求の範囲は、認証プロセスで使用され、認証トークン/クッキーの一部であるということです。現在のユーザーからクレームを削除する場合は、クライアントが新しいトークン/クッキーを取得する必要があります。

たとえば、ベアラトークンをAPIで実行している場合は、新しいトークンを生成し、そのトークンをUpdateUserGroup()からクライアントに返す必要があります。クライアントは次に、次回にAPIに要求するときに新しいトークンを使用する必要があります。

+0

アカウントコントローラに新しいトークンを生成する方法はありません。OAuthAuthorizationServerProvider – Wella

+0

@Wella OAuthAuthorizationServerOptionsは、認証オプションで初期化された後、Startup.Auth.csに静的変数として保存してから、 accountcontrollerを使用して新しいトークンを生成します。 –

+0

@Wellaコントローラで新しいトークンを生成するための答えをここで確認してください。 http://stackoverflow.com/questions/26969817/generate-token-in-controller –

関連する問題