:
internal class MyAuthProvider : OAuthBearerAuthenticationProvider
{
public override Task RequestToken(OAuthRequestTokenContext context)
if (context.Token == null)
{
var value = context.Request.Query.Get("token");
if (!string.IsNullOrEmpty(value))
{
context.Token = value;
}
}
return Task.FromResult<object>(null);
}
}
あなたはこのようなあなたのStartup.cs
でそれを使用することができます。
あなたはこのようなあなたの認証を実装しました
public void Configuration(IAppBuilder app)
{
// All the other stuff here
var audience = "";
var secret = "...";
app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
Provider = new MyAuthProvider(),
AuthenticationMode = AuthenticationMode.Active,
AllowedAudiences = new [] { audience },
IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
{
new SymmetricKeyIssuerSecurityTokenProvider("MyApp", TextEncodings.Base64Url.Decode(key))
}
});
// All the other stuff here
}
、あなたはUser.Identity
プロパティを使用して、あなたのWEBAPIコントローラにトークン情報にアクセスすることができます。カスタムの申し立てを読むには、ClaimsIdentity
にキャストしてください。
var identity = User.Identity as ClaimsIdentity;
var myClaim = identity.Claims.FirstOrDefault(c => c.Type == "myClaimKey");
以下に述べるように、私はトークンを使ってさらに何かをするつもりはなく、それを解析してクレームを抽出します。 2番目のWeb API自体には、呼び出し元を認証するためのカスタムOauthプロバイダが既にあります –