2017-03-26 6 views
0

私の現在の実装では、エンドポイントを使用して標準機能を使用してOWINトークンの実装を使用しています。OWIN変更請求トークン

e.g /token endpoint and with the below method 

and then using: 
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
{ 
     authentication code + claim assignment 
     context.Validated(ticket); 
} 

私はユーザーを偽装しようとしています。理想的には、私はGrantResourceOwnerCredentialsで自分のコードを再呼び出し/再実行できるようにしたいと思いますが、これは/トークンエンドポイントでのみ実行されるようです。または、トークンの要求を再生成し、自分のエンドポイント(たとえば、/ tokenimpersonateメソッド)で手動でそれらを送信する方法を見つけますか?

私はクッキーを使用しません。これは純粋なトークンの実装です。

もう1つの選択肢は、既存のユーザーのクレームを調整することができますが、ログアウトしてログインする必要があることです。この場合、新しいトークンをフロントエンドに渡すにはどうすればよいですか?

答えて

0

これは私が最終的にこの作業を行うために使用されるコードです:

     Authentication.SignOut(authTypeNames.ToArray()); 






        var oAuthIdentity = new ClaimsIdentity(OAuthDefaults.AuthenticationType); 
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, dbUser.Username)); 
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, dbUser.User_ID.ToString())); 
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, dbUser.UserRole)); 
        oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, dbUser.User_ID.ToString())); 



        //ads only certain docadmin ids to the role. 
        if (dbUser.UserRole == Medapp.BusinessFacade.Constants.ROLE_SECRETARY) 
        { 

         // /doc/home 

         //add guids of all the doctors as roles 
         var roles = db.OfficeAdministrators.Where(p => p.Admin_ID == dbUser.User_ID); 
         foreach (var role in roles) 
         { 
          oAuthIdentity.AddClaim(new Claim(ClaimTypes.Role, role.Doctor_ID.ToString())); 
         } 
        } 


        List<Claim> jroles = oAuthIdentity.Claims.Where(c => c.Type == ClaimTypes.Role).ToList(); 
        AuthenticationProperties properties = CreateProperties(dbUser.User_ID.ToString(), dbUser.UserRole, dbUser.Username, Newtonsoft.Json.JsonConvert.SerializeObject(jroles.Select(x => x.Value))); //user.UserName); 


        properties.IsPersistent = true; 
        properties.ExpiresUtc = new System.DateTimeOffset(new DateTime().AddDays(365), new System.TimeSpan()); 



        var ticket = new AuthenticationTicket(oAuthIdentity, properties); 

        DateTime currentUtc = DateTime.UtcNow; 
        ticket.Properties.IssuedUtc = currentUtc; 
        ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(365)); 
        string accessToken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket); 


        JObject token = new JObject(
         new JProperty("username", dbUser.Username), 
         new JProperty("token", accessToken), 
         new JProperty("uid", dbUser.User_ID.ToString()), 
         new JProperty("type", dbUser.UserRole), 
         new JProperty("roles", Newtonsoft.Json.JsonConvert.SerializeObject(jroles.Select(x => x.Value))), 
         new JProperty("access_token", accessToken), 
         new JProperty("token_type", "bearer"), 
         new JProperty("expires_in", TimeSpan.FromDays(365).TotalSeconds.ToString()), 
         new JProperty("issued", currentUtc.ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'")), 
         new JProperty("expires", currentUtc.Add(TimeSpan.FromDays(365)).ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'")) 
        ); 

        return Ok(token); 
関連する問題