2016-10-26 8 views
0

私は、(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); 

答えて

0

ID1061: HttpContext.Current is null. This code path is only valid when in the execution context of ASP.NET.

HttpContext.Current ASPNETコアでは使用できません。 Access HttpContext.Currentを参照してください。

I need this line to work so that when I try to access an action method that has my custom claim attribute, I should be able to evaluate if the claim in the token exists or not

あなたは(私はクレームがトークンに格納されていると仮定)が必要です。

  1. JWT認証ミドルウェア(あなたはすでにそれを試してみました)
  2. Claims Based Authorization

あなたはトークンに格納されていない追加の請求が必要な場合、あなたはJWT認証ミドルウェアのClaims TransformationまたはOnTokenValidatedイベントを使用することができます。

+0

私は 'Authorize'属性ではなく、' ClaimsPrinciplePermission'属性を使用しています。 aspnetコアの 'ClaimsPrinciplePermission'でそれを行う方法はありますか?またはすべてを承認する必要がありますか? – user20358

+0

あなたのケースでClaimsPrinciplePermissionを使用する特別な理由はありますか?もしそうでなければ、authorize属性を使用します。 –

+0

レガシーコードをaspnetコアに移行しています。これを行うには、多くのリワークが必要です。関連するノートでは、私がここでこの例を使ってログイン(http://andrewlock.net/introduction-to-authentication-with-asp-net-core/)とこの例(https: //docs.asp.net/en/latest/security/authorization/policies.html)を使用して受信クレームを評価します。しかし、 'AuthorizationHandlerContext context'プロパティの主張のリストは結果をもたらさない。どうしてこれなの? – user20358

関連する問題