2017-01-25 67 views
1

我對asp.net很新。我正在構建一個現有項目,其中來自js文件的用戶名和密碼被加密,並且對.aspx.cs文件執行ajax函數調用。這個ashx.cs文件解密它們,然後將它們插入到數據庫表中。現在我想包含一個名爲「Domain」的新字段,但我不知道應該如何爲它創建一個標頭,並將其從js傳遞到ashx.cs文件。我的問題可能很愚蠢,但我一直掙扎了一段時間,並感謝一些幫助。我嘗試使用context.Response.AddHeader爲域,但我不知道我應該通過它進入什麼值。如何添加一個新的頭文件到context.response在asp.net

「運行」和「RUP」似乎已經創建了某處,我不能找到它們遵循域頭一樣..

js文件:

function Register() { 

    if (ValidateRegisterWindow()) { 

     var URL = "SecureChatServer.ashx"; 
     if (URL == null) { alert("Request URL is Empty"); } 
     else { 
      username = Encrypt(document.getElementById('NewUserName').value, EncryptionKey); 
      password = Encrypt(document.getElementById('NewPassword').value, EncryptionKey); 
      domain = Encrypt(document.getElementById('NewDomain').value, EncryptionKey); 

      AjaxRequest(ProcessRegisterResponse, URL, "POST", '', '', { RequestCode: 'SC006', RUN: username, RUP: password}); //how to pass domain here?? 
     } 
    } 

} 


function ProcessRegisterResponse() { 

    var ResponseStatus = GetHeader(ResponseHeaderJSON, 'ResponseStatus'); 
    if (ResponseStatus == "RS-OK") { 

     ShowAlertMessage("Registration Sucessful", "", "User Registered and Logged in Sucessfully"); 
     CurrentUser = document.getElementById('NewUserName').value; 
     LoginEvents(CurrentUser, true); 
     ClearRegisterWindow(); 


    } 
    else if (ResponseStatus == "RS-Failed") { 

     ShowErrorBox("Registration Error", "This username cannot be registered ,please try a different username"); 

    } 
    else { 

     ShowErrorBox("Unknown Error :Code-01 " + ResponseStatus, "Request cannot be processed ,please try again."); 

    } 
} 

的ashx.cs文件:

#region Handle Add New User Request 
      case "SC006": //indicates request to add new user 
       { 
        string UserName, Password; 
        UserName = Decrypt(context.Request.Headers["RUN"], EncryptionKey); 
        Password = Decrypt(context.Request.Headers["RUP"], EncryptionKey); 
        Users newuser = new Users(); 

        try 
        { 
         if (newuser.AddUser(UserName, Password, SessionID, UserIPAddress)) 
         { 
          context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-OK'"); 
         } 
         else 
         { 
          context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-Failed'"); 
         } 
        } 
        catch (Exception e) 
        { 
         Debug.WriteLine("Failed Request SC006 : " + e.ToString()); 
         context.Response.AddHeader("CustomHeaderJSON", "ResponseStatus:'RS-Exception'"); 

        } 
       } 
       break; 

      #endregion 
+0

.NET框架3.5 – user3387677

回答

0

它很容易,你可以使用此代碼添加頁眉

context.Response.Headers.Add(「your header here」);

+0

是的,我嘗試過,但我不知道什麼值傳遞給NameValueCollection .. – user3387677

+0

你能解釋一點嗎? –

+0

謝謝,我創建了一個新的NameValueCollection並通過它。有用! – user3387677

相關問題