2016-11-25 12 views
6

でデコードします。私のWeb Api 2.2 OWINベースのアプリケーションでは、手動でベアラトークンをデコードする必要がありますが、これを行う方法はわかりません。 これは、手動でパラメータとして渡されたトークンからのクレームをデコードして取得するにはどのようにパラメータとして手動でOAuthベアラトークンをC#

[RoutePrefix("api/EP")] 
public class EPController : MasterController 
{ 
    [HttpGet] 
    [AllowAnonymous] 
    [Route("DC")] 
    public async Task<HttpResponseMessage> GetDC(string token) 
    { 
     //Get the claim identity from the token here 
     //Startup.OAuthServerOptions... 

     //..and other stuff 
    } 
} 

をベアラトークンを送る私のコントローライムで

public class Startup 
{ 
    public static OAuthAuthorizationServerOptions OAuthServerOptions { get; private set; } 
    public static UnityContainer IoC; 
    public void Configuration(IAppBuilder app) 
    { 
     //Set Auth configuration 
     ConfigureOAuth(app); 

     ....and other stuff 
    } 

    public void ConfigureOAuth(IAppBuilder app) 
    { 
     OAuthServerOptions = new OAuthAuthorizationServerOptions() 
     { 
      AllowInsecureHttp = true, 
      TokenEndpointPath = new PathString("/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
      Provider = new AuthProvider(IoC.Resolve<IUserService>(), IoC.Resolve<IAppSettings>()) 
     }; 

     // Token Generation 
     app.UseOAuthAuthorizationServer(OAuthServerOptions); 
     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 
    } 
} 

私startup.csのですか?

NOTE:私は、ヘッダーにトークンを送信し、[承認]を使用して、(ClaimsIdentity)User.Identityのなどすることができます知っているが、問題は、それがヘッダに提示していないときに、トークンを読み取る方法です。

答えて

2

です。 ソースコードを見ることができます。ただ、将来的に訪問するかもしれない他の人のためにここにこれを置く

Bearer-Token-Deserializer

+0

ありがとう、ちょうど私が探していたもの! Awesome –

+0

上記はうまくいくかもしれませんが。より簡単な解決策はhttps://long2know.com/2015/05/decrypting-owin-authentication-ticket/ –

0

System.IdentityModel.Tokens.Jwtパッケージ - https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/を使用して、JWTを読み取り、プリンシパルとアイデンティティオブジェクトを作成できます。

ここでトークンを読み取り、検証時のオプションが利用可能を示す簡単な例では、私はMachineKeyDataProtectorを使用して暗号化されているベアラ・トークンを、デシリアライズのためのサンプルプロジェクトを作成し、

private ClaimsIdentity GetIdentityFromToken(string token, X509Certificate2 certificate) 
    { 
     var tokenDecoder = new JwtSecurityTokenHandler();   
     var jwtSecurityToken = (JwtSecurityToken)tokenDecoder.ReadToken(token); 

     SecurityToken validatedToken; 

     var principal = tokenDecoder.ValidateToken(
      jwtSecurityToken.RawData, 
      new TokenValidationParameters() 
       { 
        ValidateActor = false, 
        ValidateIssuer = false, 
        ValidateAudience = false, 
        ValidateLifetime = false, 
        ValidateIssuerSigningKey = false, 
        RequireExpirationTime = false, 
        RequireSignedTokens = false, 
        IssuerSigningToken = new X509SecurityToken(certificate) 
       }, 
      out validatedToken); 

     return principal.Identities.FirstOrDefault(); 
    } 
+0

ベアラトークンは、JWTのトークンではありません。 jwtトークンは、header.payload.signatureのようになります。私が得ているベアラトークンにはドットは含まれておらず、base64はエンコードされていません。 – Marius

4

https://long2know.com/2015/05/decrypting-owin-authentication-ticket/にある解決策は簡単です。

わずか2行:asp.netのIDで

var secureDataFormat = new TicketDataFormat(new MachineKeyProtector()); 
AuthenticationTicket ticket = secureDataFormat.Unprotect(accessToken); 



private class MachineKeyProtector : IDataProtector { 
    private readonly string[] _purpose = 
    { 
     typeof(OAuthAuthorizationServerMiddleware).Namespace, 
     "Access_Token", 
     "v1" 
    }; 

    public byte[] Protect(byte[] userData) 
    { 
     throw new NotImplementedException(); 
    } 

    public byte[] Unprotect(byte[] protectedData) 
    { 
     return System.Web.Security.MachineKey.Unprotect(protectedData, _purpose); 
    } } 
+0

にあります。私はこの解決策を試していませんが、あなたの時間を費やしてあなたの解決策を閉じた質問+1 –

関連する問題