2012-01-16 6 views
1

私はファイルを転送するWebサービスを持っています。転送元がアクセス権を持っていることを認証します。私はすべてのクライアントをADから引き出されたユーザSIDで認証し、それを暗号化してDBに保存します。私が実行している問題は、ファイルを転送するクライアントが49152バイトごとにサービスを呼び出していることです。だから、基本的には、新しいバイト配列が来るたびにDB呼び出しをしたくない。暗号化されたSIDが一度認証された後、それが完全になるまでそれを信頼するには、あなたの唯一の懸念は、頻繁デシベルに当たっている場合は、私が認証されたとして、あなたのWebサービスでSIDをキャッシュ示唆しファイルを転送するためのWebサービスを認証しますか?

public class TransferFile : System.Web.Services.WebService 
    { 
     int Authenticated = 0; 
     [WebMethod] 
     public void WriteBinaryFile(string userSID, byte[] buffer, string FileName) 
     { 

      string ConnectionString = null; 
      string DBServer = null; 
      string AuthenticationMethod = null; 
      string DB_U = null; 
      string DB_P = null; 
      string DBName = null; 
      try 
      { 
       XmlReader xmlReader = XmlReader.Create(@"C:\Program Files\SM\SM_DB_Config.xml"); 
       while (xmlReader.Read()) 
       { 
        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "DB_Server")) 
        { 
         string strUsername = null; 
         strUsername = xmlReader.ReadInnerXml().ToString(); 
         if (strUsername.ToString() == "") 
         { 
         } 
         else 
         { 
          DBServer = SpartaCrypto.SpartaDecryptAES(strUsername, "secretcode"); 
         } 
        } 
        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "DB_Name")) 
        { 
         string strUsername = null; 
         strUsername = xmlReader.ReadInnerXml().ToString(); 
         if (strUsername.ToString() == "") 
         { 
         } 
         else 
         { 
          DBName = SpartaCrypto.SpartaDecryptAES(strUsername, "secretcode"); 
         } 
        } 
        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "DB_AuthenticationMethod")) 
        { 
         string strUsername = null; 
         strUsername = xmlReader.ReadInnerXml().ToString(); 
         if (strUsername.ToString() == "") 
         { 
         } 
         else 
         { 
          AuthenticationMethod = SpartaCrypto.SpartaDecryptAES(strUsername, "secretcode"); 
         } 
        } 
        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "DB_U")) 
        { 
         string strUsername = null; 
         strUsername = xmlReader.ReadInnerXml().ToString(); 
         if (strUsername.ToString() == "") 
         { 
         } 
         else 
         { 
          DB_U = SpartaCrypto.SpartaDecryptAES(strUsername, "secretcode"); 
         } 
        } 
        if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "DB_P")) 
        { 
         string strUsername = null; 
         strUsername = xmlReader.ReadInnerXml().ToString(); 
         if (strUsername.ToString() == "") 
         { 
         } 
         else 
         { 
          DB_P = SpartaCrypto.SpartaDecryptAES(strUsername, "secretcode"); 
         } 
        } 

       } 
       xmlReader.Close(); 
       if (AuthenticationMethod == "Integrated") 
       { 
        ConnectionString = "Data Source=" + DBServer + ";Provider=SQLOLEDB;Initial Catalog=" + DBName + ";Integrated Security=SSPI;"; 
       } 
       else 
       { 
        ConnectionString = "Data Source=" + DBServer + ";Provider=SQLOLEDB;Initial Catalog=" + DBName + ";User ID=" + DB_U + ";Password=" + DB_P; 
       } 
       String query = "SELECT COUNT(AD_SID) As ReturnCount FROM AD_Authorization WHERE AD_SID = ?"; 
       OleDbConnection conn = new OleDbConnection(ConnectionString); 

       OleDbCommand cmd = new OleDbCommand(query, conn); 

       cmd.Parameters.AddWithValue("userSID", userSID.ToString()); 
       conn.Open(); 
       int returnCount = (Int32)cmd.ExecuteScalar(); 
       conn.Close(); 
       if (returnCount >= 1) 
       { 
        Authenticated = 1; 

       } 
       else 
       { 
        Authenticated = 0; 
       } 
      } 
      catch (Exception ex) 
      { 

      } 

      if (Authenticated == 1) 
      { 
       string PathName = @"C:\Test\"; 


       using (FileStream fs = new FileStream(PathName + FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) 
       { 
        fs.Seek(0, SeekOrigin.End); 
        fs.Write(buffer, 0, buffer.Length); 
       } 
      } 
     } 
    } 

答えて

0

は、ここに私のコードです。要求を受け取ったときはいつでも、値がキャッシュに存在するかどうかをチェックし、値がキャッシュに存在しない場合は、dbにヒットします。また、有効期限を設定するのに最適な時間を設定する必要があります。

この目的でHttpRuntime.Cacheを使用できます。

たぶん、あなたはASP.NETキャッシュの最初の結果を保存することができ、キャッシュ

HttpRuntime.Cache.Get(userSID) 
0

から値を取得するために、キャッシュ

HttpRuntime.Cache.Insert(userSID, 1, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0)) 

コードに値を設定するコード。あなたはこのようにそれらを回復することができ、

var Authenticated = ... (the value that you already have) 

HttpContext.Current.Cache.Insert(
        "myAuthenticatedCacheKey", 
        Authenticated, 
        null, DateTime.Now.AddMinutes(10), // 10 minutes expiration 
        System.Web.Caching.Cache.NoSlidingExpiration 
        ); 

:あなたはこのようにキャッシュに(検証変数のような)オブジェクトを保存することができますhttp://msdn.microsoft.com/en-us/library/aa480499.aspx

を参照してください

var Authenticated = HttpContext.Current.Cache.Get("myAuthenticatedCacheKey"); 

行くための別の方法を使用することができアプリケーションオブジェクト(セッションオブジェクトと同じですが、アプリケーションスコープがあり、すべてのセッションで共有されます)。あなたは、次のようなアプリケーションのスコープからオブジェクトを格納および取得することができます

Session("myAuthenticatedSessionKey") = Authenticated; 
... 
var Authenticated = Session("myAuthenticatedSessionKey"); 

キャッシュまたはアプリケーションオブジェクトからそれらを取得するとき、あなたがオブジェクトをキャストする必要がことに注意してください。

関連する問題