2016-11-23 7 views
0

を暗号化するために、私は のようなものを見るために、ユーザーにしたいクエリ文字列パラメータを、暗号化するためにネットコアミドルウェアを書いています?ENC = VXzal017xHwKKPolDWQJoLACDqQ0fE // wGkgvRTdG/GgXIBDd1 コードは、この を見ながら?ユーザー= 123 &アカウント= 456。ミドルウェアは、クエリ文字列

私はIDataProtectorを使用してパラメータを暗号化します。私のミドルウェアでの呼び出しは()、以下のコード

if (UriHelper.GetEncodedUrl(context.Request).Contains("?")) 
     { 
      string query = ExtractQuery((context.Request.GetEncodedUrl())); 

      int indexOfEnc = query.IndexOf(PARAMETER_NAME, StringComparison.OrdinalIgnoreCase); 

      if (indexOfEnc > -1) 
      { 

       var enc = context.Request.Query[PARAMETER_NAME]; 
       enc = Decrypt(enc); 
       context.Request.Path = new PathString(context.Request.Path.Value + enc); 
       await _next.Invoke(context); 
      } 

      else if (context.Request.Method == "GET" || context.Request.Method == "POST") 
      { 
       // Encrypt the query string and redirects to the encrypted URL. 
       // Remove if you don't want all query strings to be encrypted automatically. 
       string encryptedQuery = Encrypt(query); 
       string tempRawUrl = UriHelper.GetEncodedUrl(context.Request).ToLower(); 
       if (!(context.Request.Method == "POST" && tempRawUrl.Contains("ha"))) 
       { 
        context.Response.Redirect(context.Request.Path.Value + "?" + PARAMETER_NAME + "=" + encryptedQuery); 
       } 
      } 

     } 
     else 
     { 
      await _next.Invoke(context); 
     } 

私は、ログイン初めてのように見え、ユーザー/パスを入力したコードは、上記のelseif節に入って来、細かい暗号化されます。セクションには、何もしない場合、私は次回のparam「ENC」クエリを探し、それを復号化しますしながらパスがよさそうだ、中

**await _next.Invoke(context);** 

。私はそれがユーザー/パスを検証するためにコントローラに行くことを期待しています。

ここに私と一緒にいらしてください。これは私の最初のミドルウェアで、私のレガシーコードでhttphandlersを置き換えようとしています。

何か助けていただければ幸いです。私はこれに約5時間を費やしており、それを理解することはできないようです。

+0

質問には関係ありませんが、クエリ文字列を暗号化して解決しようとしている問題はありますか? – CodeCaster

+0

クライアントIDだけでなくクエリ文字列にクライアント固有の秘密鍵を渡す必要があり、プレーンテキストとして入力する必要はありません。 – Vish

答えて

0

コードをに変更しました。

if (indexOfEnc > -1) 
      { 

       var enc = context.Request.Query[PARAMETER_NAME]; 
       enc = "?" + Decrypt(enc); 
       Microsoft.AspNetCore.Http.QueryString queryString = new Microsoft.AspNetCore.Http.QueryString(enc); 
        context.Request.QueryString = queryString; 

       await _next.Invoke(context); 
      } 

となりました。私はまだここに何かがないと思っています。これを行うより良い方法はありますか?

1

IQueryFeatureIResponseFeatureをご覧ください。 ASP.NETコアでは、基本オブジェクトの動作をオーバーライドできます。 HttpRequest & HttpResponseオブジェクト。

透過復号化のために既存のIQueryFeatureを単にラップすることができます。 照会の暗号化では、既存のIResponseFeatureを透過的な暗号化用にラップします。 ミドルウェア内にラッパーを設定します。

httpContext.Features.Set<IQueryFeature>(new TransparentDecryptionQueryFeature(httpContext.Features.Get<IQueryFeature>)); 
httpContext.Features.Set<IResponseFeature>(new TransparentEncryptionResponseFeature(httpContext.Features.Get<IResponseFeature>)); 

このようにすると、あなたの後に実行されるすべてのミドルウェアが「透過的機能」を使用します。

public class TransparentDecryptionQueryFeature : IQueryFeature 
{ 
    privare readonly IQueryCollection _store; 

    public TransparentDecryptionQueryFeature(IQueryFeature feature) 
    { 
     _store = new TranparentDecryptionQueryCollection(feature.Query); 
    } 

    public IQueryCollection Query 
    { 
     get 
     { 
      return _store; 
     } 

     set 
     { 
      _store = new TransparentDecryptionQueryCollection(value); 
     } 
    } 
} 

public class TransparentDecryptionQueryCollection : IQueryCollection 
{ 
    private readonly IQueryCollection _inner; 

    public TransparentDecryptionQueryCollection(IQueryCollection inner) 
    { 
     var store = new Dictionary<string, StringValues>() 
     foreach (var item in inner) 
     { 
      if (item.Key == PARAMETER_NAME) 
      { 
       // TODO : Adds all the decrypted query parameters in the store 
      } 
      else 
      { 
       store.Add(item); 
      } 
     } 
     _inner = new QueryCollection(store); 
    } 

    // implement other methods by delegating with _inner object 
}