如何在 asp.net 中向 context.response 添加新的 header
how to add a new header to context.response in asp.net
我对 asp.net 很陌生。我正在构建一个现有项目,其中 js 文件中的用户名和密码已加密,并且对 .aspx.cs 文件进行了 ajax 函数调用。这个 ashx.cs 文件解密它们,然后将它们插入数据库 table。现在我想包含一个名为 "Domain" 的新字段,但我不知道应该如何为它创建一个 header,以便将它从 js 传递到 ashx.cs file.My 问题可能很愚蠢,但我已经为此苦苦挣扎了一段时间,并感谢您的帮助。我尝试对域使用 context.Response.AddHeader,但我不知道应该向其中传递什么值..
"RUN" 和 "RUP" 似乎已经在某处创建,我无法找到它们以遵循相同的域 header..
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
您可以使用此代码轻松添加 header
context.Response.Headers.Add ("your header here ");
我对 asp.net 很陌生。我正在构建一个现有项目,其中 js 文件中的用户名和密码已加密,并且对 .aspx.cs 文件进行了 ajax 函数调用。这个 ashx.cs 文件解密它们,然后将它们插入数据库 table。现在我想包含一个名为 "Domain" 的新字段,但我不知道应该如何为它创建一个 header,以便将它从 js 传递到 ashx.cs file.My 问题可能很愚蠢,但我已经为此苦苦挣扎了一段时间,并感谢您的帮助。我尝试对域使用 context.Response.AddHeader,但我不知道应该向其中传递什么值..
"RUN" 和 "RUP" 似乎已经在某处创建,我无法找到它们以遵循相同的域 header..
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
您可以使用此代码轻松添加 header
context.Response.Headers.Add ("your header here ");