2016-10-30 8 views
1

私は自分のasp.netフレームワークをasp.netコアに変換しています。オブジェクトをauthenticationcontext asp.netコアに保存

私が直面していることの1つは、照会データを認証ハンドラの認証コンテキストに保存することです。 、私はASP.Net Frameworkで私AuthorizeAttributeでやった私のasp.netフレームワークで

public override void OnAuthorization(HttpActionContext actionContext) 
     { 
      // Retrieve email and password. 
      var accountEmail = 
       actionContext.Request.Headers.Where(
        x => 
         !string.IsNullOrEmpty(x.Key) && 
         x.Key.Equals(HeaderFields.RequestAccountEmail)) 
        .Select(x => x.Value.FirstOrDefault()) 
        .FirstOrDefault(); 

      // Retrieve account password. 
      var accountPassword = 
       actionContext.Request.Headers.Where(
        x => 
         !string.IsNullOrEmpty(x.Key) && 
         x.Key.Equals(HeaderFields.RequestAccountPassword)) 
        .Select(x => x.Value.FirstOrDefault()).FirstOrDefault(); 


      // Invalid account name or password. 
      if (string.IsNullOrEmpty(accountEmail) || string.IsNullOrEmpty(accountPassword)) 
      { 
       // Treat this request is unauthorized. 
       actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new 
       { 
        Error = $"{Language.WarnAccountNotLogin}" 
       }); 

       return; 
      } 

      // Find the hashed password from the original one. 
      var accountHashedPassword = RepositoryAccountExtended.FindMd5Password(accountPassword); 

      // Retrieve person whose properties match conditions. 
      var person = RepositoryAccountExtended.FindPerson(null, accountEmail, accountHashedPassword, null, null); 

      // No person has been found. 
      if (person == null) 
      { 
       // Treat this request is unauthorized. 
       actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new 
       { 
        Error = $"{Language.WarnAccountNotLogin}" 
       }); 
       return; 
      } 

      // Account has been disabled. 
      if ((StatusAccount) person.Status == StatusAccount.Inactive) 
      { 
       // Treat the login isn't successful because of disabled account. 
       actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new 
       { 
        Error = $"{Language.WarnDisabledAccount}" 
       }); 

       return; 
      } 

      // Account is still pending. 
      if ((StatusAccount) person.Status == StatusAccount.Pending) 
      { 
       // Treat the login isn't successful because of pending account. 
       actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, new 
       { 
        Error = $"{Language.WarnPendingAccount}" 
       }); 

       return; 
      } 

      // Account role isn't enough to access the function. 
      if (!Roles.Any(x => x == person.Role)) 
      { 
       // Role isn't valid. Tell the client the access is forbidden. 
       actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, new 
       { 
        Error = $"{Language.WarnForbiddenAccessMethod}" 
       }); 
      } 

      // Store the requester information in action argument. 
      actionContext.ActionArguments[HeaderFields.Account] = person; 
     } 

見ての通り、私は私のクエリデータ保存 - するactionContextに(アカウントをこのような状況では)私は後でそれをコントローラにアクセスすることができます。

私の質問は次のとおりです。すべてのAuthorizationHandlerでデータベースを照会したくないので、私はASP.NET Coreでどのように同じことを達成できますか?

はまず

答えて

1

どのように私はASP.NETのコアで同じことを達成することができます

、ありがとう、あなたのケースのために、それは基本認証であってもよいし、認証ミドルウェアを必要としています。 Aspnet Coreの場合、組み込みの基本認証ミドルウェアはありません。 solutonがhereであるか、thisのような独自の認証ミドルウェアを実装できます。

actionContextの に私のクエリデータ(この状況ではアカウント)を保存しました。後でそのデータにアクセスできます。

はこれを実装するには、現在のUser.IdentityにHttpContext.Items

  • 追加請求に

    1. 追加パラメータあなたはClaimsTransformationまたはカスタムを使用することができます。

  • 2つの方法が私の心に来ています認証ミドルウェアの後のミドルウェア。独自の実装を使用する場合は、HandleAuthenticateAsyncメソッドを使用することもできます。

    更新

    クエリデータを保存するための適切な場所がHandleAuthenticateAsyncあるようです。あなたはblowdartのbasic authenticationソリューション@使用している場合は、あなたのコードは以下のようなものかもしれません:

      ..... 
          await Options.Events.ValidateCredentials(validateCredentialsContext); 
    
          if (validateCredentialsContext.Ticket != null) 
          { 
           HttpContext.Items[HeaderFields.Account] = person; // assuming you retrive person before this 
           Logger.LogInformation($"Credentials validated for {username}"); 
           return AuthenticateResult.Success(validateCredentialsContext.Ticket); 
          } 
    
    +0

    は道を輝いていただきありがとうございます。私は私のアプリケーションでJWTを使用しているので、私はHttpContext.Itemsを選択すると思います:) – Redplane

    関連する問題