user3387677
user3387677

Reputation: 47

how to add a new header to context.response in asp.net

I'm very new to asp.net. I am building on an existing project where the username and password from the js file are encrypted and an ajax function call is made to the .aspx.cs file. This ashx.cs file decrypts them and and then inserts them into the database table. Now i want to include a new field called "Domain" but i have no clue as to how i should create a header for it, to pass it from the js to the ashx.cs file.My question might be silly, but Ive been struggling with it for a while and appreciate some help. I tried using context.Response.AddHeader for the domain, but i dont know what values i should pass into it..

"RUN" and "RUP" seems to have been already created somewhere and im not able to locate them to follow the same for domain header..

js file:

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.");

    }
}

The ashx.cs file :

  #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

Upvotes: 1

Views: 2496

Answers (1)

salem albadawi
salem albadawi

Reputation: 67

its easy you can use this code to add the header

context.Response.Headers.Add ("your header here ");

Upvotes: 1

Related Questions