私は、(VS2013)MVC5で書かれた古いアプリケーションをAngular 1で(VS2015)MVC 6にAngular 2で移行しています。私はその後、次のコード行を使用して、セッションに格納するのに使用されるWeb UI側のポストアクションメソッドでは、クライアントからのトークンを受け取る:AngularからAspnetコア1.0 Webサイトに送信されたJWTを使用する
//Store token details
[HttpPost]
public void EvaluateToken(string token)
{
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
SecurityToken st = tokenHandler.ReadToken(token);
var tokenData = ((JwtSecurityToken)st);
var claims = tokenData.Claims.Where(t => t.Type.Equals(" ")).ToList();
ClaimsIdentity cIdentity = new ClaimsIdentity(DefaultAuthenticationTypes.ExternalBearer);
ci.AddClaims(claims);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, cIdentity);
string user = "//get username from token";
ClaimsPrincipal cPrincipal = new ClaimsPrincipal (cIdentity);
//write to cookie
SessionSecurityToken sst = new SessionSecurityToken(cPrincipal , TimeSpan.FromMinutes(20));
sst.IsReferenceMode = true;
SessionAuthenticationModule ssm = FederatedAuthentication.SessionAuthenticationModule;
ssm.WriteSessionTokenToCookie(sst);
}
上記のコードは、非aspnetcoreのプロジェクトに適していますし、私次のコード行を使用して認可コンテキストからクレームを取り出すことができます。action
は、呼び出されているそれぞれのアクションメソッドに対する属性に設定されたクレームを含み、指定されたクレームの真偽を含むval
n context.Principal
。私EvaluateTokenメソッドのコードのこの行の
ID1061: HttpContext.Current is null. This code path is only valid when in the execution context of ASP.NET.
:
public class CustomClaimsAuthorizationManager: ClaimsAuthorizationManager
{
public override bool CheckAccess(AuthorizationContext context)
{
var action = context.Action.First().Value;
bool val= context.Principal.HasClaim(" ", action);
return val;
}
}
コアプロジェクトに移動するときしかし、私はこのエラーを取得する
SessionAuthenticationModule ssm = FederatedAuthentication.SessionAuthenticationModule;
私がこの行を必要とします私のカスタムクレーム属性を持つアクションメソッドにアクセスしようとすると、トークンのクレームが存在するかどうかを評価できるはずです。このままaspnetコアに移動する方法がない場合は、aspnetコアでトークンを使用して、渡されたトークンとオーディエンス、発行者、秘密鍵のみを持つ特定のビューへのアクセスを許可する方法を知りたいトークンを生成するために使用されます。ドキュメントでカスタムポリシーを追加することが指定されていて、別のパスを取っているようです。私は、そのようなロジックを使用して、UI側の主オブジェクトに要求を格納する方法がわからないので、UI側のビュー間を移動するときに使用できます。
これは私がConfigureメソッドでASPNETコアWebプロジェクトで私startup.csファイルに持っているものされています
var issuer = "localhost";
var aud = "localhost";
var secret = "SecretValueHere";
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secret));
var jwtBearerOptions = new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
Audience = aud ,
ClaimsIssuer = issuer,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = signingKey,
ValidateIssuer = true,
ValidIssuer = issuer,
ValidateAudience = true,
ValidAudience = aud ,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
RequireExpirationTime = true
}
};
app.UseJwtBearerAuthentication(jwtBearerOptions);
私は 'Authorize'属性ではなく、' ClaimsPrinciplePermission'属性を使用しています。 aspnetコアの 'ClaimsPrinciplePermission'でそれを行う方法はありますか?またはすべてを承認する必要がありますか? – user20358
あなたのケースでClaimsPrinciplePermissionを使用する特別な理由はありますか?もしそうでなければ、authorize属性を使用します。 –
レガシーコードをaspnetコアに移行しています。これを行うには、多くのリワークが必要です。関連するノートでは、私がここでこの例を使ってログイン(http://andrewlock.net/introduction-to-authentication-with-asp-net-core/)とこの例(https: //docs.asp.net/en/latest/security/authorization/policies.html)を使用して受信クレームを評価します。しかし、 'AuthorizationHandlerContext context'プロパティの主張のリストは結果をもたらさない。どうしてこれなの? – user20358