How to pass HTTP post parameters - c#

In my asp.net application one webpage page_load event
protected void Page_Load(object sender, EventArgs e)
{
try
{
string data = string.Empty;
// Determine if session has Single Sign On credentials
if (Request.Form["Rams"] != null)
{
data = RamsLogin();
}
I would like to debug this page from outside the applicaton, How to pass Rams parameter via HTTP post to execute the RamsLogin() method?

The Page_Load event runs every time the page loads. In your case, the SAMLlogin() method will execute whenever a client submit a POST request and inform some value into the samlResponse variable.
That request is usually made using HTML <form method="POST"> and the samlResponse variable would be an INPUT, SELECT or TEXTAREA element with the name="samlResponse" attribute.
I say usually because a program can simulate the same behavior without using any HTML at all.

Related

Get value from another page

We are using an application that runs on asp.net and C#. When a user is logged in, their profile information is displayed (name, id number) on a Profile page (information comes from MSSQL). I would like to get the id number for the logged in user and pass that value to an embedded form (this part I know I can do), the struggle is figuring out how to get the id number from one page to the other page.
I am not proficient with C# or asp.net but I did find where the id value is at, I am not sure how to call it from my form page?
Pass Values Between ASP.NET Web Forms Pages
The following options are available even if the source page is in a different ASP.NET Web application from the target page, or if the source page is not an ASP.NET Web Forms page:
Use a query string
Get HTTP POST information from the source page
The following options are available only when the source and target pages are in the same ASP.NET Web application:
Use session state
Create public properties in the source page and access the property
values in the target page
Get control information in the target page from controls in the
source page
The main factor that should influence your approach should be the fact that this is a secure application and you do not want the information being passed to be prone to scrutiny.
First Option:
Use Cookies
On your firstPage.aspx
HttpCookie appCookie = new HttpCookie("IdValue");
appCookie.Value = "The value of your id here" or id.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(appCookie);
To get:
SecondPage.aspx
HttpCookie aCookie = Request.Cookies["IdValue"];
String getValue = aCookie.Value.ToString();
Cookies expires depends on your Settings here appCookie.Expires = DateTime.Now.AddDays(1); it can be AddDays('MoreNumberofdays') like AddDays(4) it will expires in 4 days. Something like that.
some additional Guide
Second Option:
Class:
public class Class1
{
public static string storevalue;
}
Page1.aspx
protected void Page_Load(object sender, EventArgs e)
{
Class1.storevalue = "This is the value from page1";
Response.Redirect("WebForm2.aspx");
}
Page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
string getvalue = Class1.storevalue;
}

ASP.Net code behind wait a ADO.Net query to database returns for redirecting

I have a asp.net webform page who uses 2 google charts, I'm using Jquery Ajax to retrieve information from database to populate the charts, the query to retrieve the chart data takes a long time, in this time user can make a click in a link to redirect to another page, the redirect is very slow because wait querys return from database to do the redirect, how can I avoid this behavior?
I'm using Jquery Datatable plugin, when user click one row an javascript event is called
function redirectToCampaignCreator(campaignID) {
try {
$.xhrPool.abortAll();
redirecting = true;
$('#multiCampID').val(campaignID);
//window.location.href = "campaigncreator.aspx?Edit=True&CampaignId=" + campaignID;
DoPartialPostBack("<%=btnCampaignEdit.UniqueID%>");
} catch (e) {
}
}
In this function I cancel the Ajax calls and do a __doPostBack(control, ""); to ejecute some code to save session values and then redirect
protected void Page_PreInit(object sender, Even`enter code here`tArgs e)
{
if (IsPostBack)
{
if (Request.Form["multiCampID"] != null)
{
View();
Context.Response.Redirect("CampaignCreator.aspx?Edit=True", true);
}
}
}
The redirect is slow because it's waiting querys return from DB
Can you make your link client side instead of linking via redirect in the code behind?
Link to other page
I am not sure how you are using the code structure to manage such kind of tasks but if you want that your responsiveness to be free you can use async your c# functions.
https://msdn.microsoft.com/en-us/library/hh191443.aspx

How can I read the current headers without making a new request with C#?

Since JS cannot get the initial page request, can C# get the current headers without making a new request?
The main goal is to be able to determine who is accessing the page by looking at the HTTP header that has a custom value "uid": "world" that is passed in. After determining it, the value is then displayed on that same page.
Hello "world"
Can this be done in C#? How?
To access the headers within your MVC controller, you can use this:
Controller
public ActionResult Index()
{
var uid = System.Web.HttpContext.Current.Request.Headers["uid"];
// Add your view model logic here...
var model = new TestModel
{
HeaderValue = uid
};
return View(model);
}
View
#model MyProject.Models.TestModel
<div>
Hello #Model.HeaderValue
</div>
In ASP.NET, you can read the Headers from the Request object. My answer is assuming Web Forms, please update your question if you're not using Web Forms (it's important to include that information in your initial question in the future).
<asp:Label runat="server" id="HeaderLbl" />
And code behind:
using System.Web; //add this to the top of your class if it's not already there
protected void Page_Load(object sender, EventArgs e)
{
HeaderLbl.Text = Request.Headers["uid"];
}
In your controller, use System.Mvc.Web.Mvc.Request.Headers to get the headers from the current request.
If you are using Razor, you can include #Request.Headers directly on your .cshtml page and share the contents with Javascript, if you want.

Calling url on base of incoming url

I am trying to detect incoming url in asp.net page and making some decision on the base of that url But I am facing some problem here is my c# code the detect the url and also condtions
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String url = Request.ServerVariables["HTTP_REFERER"];
if (url != "http://172.17.0.221:84/CP.aspx")
{
Response.Redirect("http://a.sml.com.pk");
}
else
{
Response.Redirect("http://172.17.0.221:85/MilkSale.aspx");
}
}
}
</script>
But When I call the page from http://172.17.0.221:84/CP.aspx then it gives this error:
This webpage has a redirect loop.
The webpage at http://172.17.0.221:85/MilkSale.aspx has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
Can any one tell me what may the error in this code?
If your script statement is also on the MilkSale.aspx page, then it will fire every time the page is hit; in effect, it will redirect to itself forever (or, in this instance, until asp.net detects that it is requesting the same page over and over again).
To begin with:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String url = Request.ServerVariables["HTTP_REFERER"];
if(!String.IsNullOrEmpty(url))
{
if (!url.ToUpper().Contains("CP.ASPX"))
{
Response.Redirect("http://a.sml.com.pk");
}
else if (!url.ToUpper().Contains("MILKSALE.ASPX") && !url.ToUpper().Contains("CP.ASPX"))
{
Response.Redirect("http://172.17.0.221:85/MilkSale.aspx");
}
}
}
}
Then this will fix the first issue. However, you then have to consider some other issues with your code;
You are doing case insensitive string matching.
You have IP addresses hard coded in your urls
1) is pretty easy to use; you can use String.Compare(url, referrer, StringComparison.InvariantCultureIgnoreCase) for example. In my code, I have used .ToUpper() but this is still fraught with issues (but makes for a compact example)
2) Is more difficult; you should really disassociate your redirect mechanism from the root url, or else you'll have to change your code everytime you change site. Either use the property HttpContext.Current.Request.Url.PathAndQuery or, preferably, look at URL rewriting.

A variable for user ID, reachable during the whole process of page creation.(instead of session)

I am currently using session to hold the user ID at my web application. And i read a lot about sessions is evil, so my plans is to find another solution.
So my next step is to use encrypted cookie.
Something like:
userInformation: ENCRYPT(UserID,subdomain,someComputerUniqueValue,hashvalueOftheString)
each user has their own subdomain, so the UserID and Subdomain must match.
But. Now at almost every page i call the session value to get the userID.
I want to change this to some kind of variable, but what kind of variable?!
I am now setting the session value inside a httpmodule. in the
public void Application_PreBeginRequest
Is it possible to create a variable within application_prebeginRequest and read it somewhere else during the creation of the page. for example in the masterpage, och the contentpage. or the classes that is used at that specific page.
WHen the page is created and sent to the client, the variable should die.
What kind of variable am i looking for? is it global variable? if not, what is global variable?
Thanks for reading!
Mattias R.
Edit:
This cookie is not for authentication. I want to save the ID of the user connected to the subdomain, so i dont have to run the "SELECT ID from account where subdomain='somethin'" query each time a page is visited.
You can store what you need inside the HttpContext.Current.Items. Items put inside that will live only during the current web request and will be available globally in your web application.
// Global.asax
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Items["hello"] = DateTime.Now;
}
// Default.aspx
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = HttpContext.Current.Items["hello"].ToString();
}
}
By the way, at Application_BeginRequest event, the Session object isn't available.
For more information about HttpContext.Current.Items, look at https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx.
Once the user is authenticated, why don't you log them in with FormsAuthentication.SetAuthCookie?
You can then retrieve the currently logged in user using HttpContext.Current.User.Identity.Name.
Session is not "evil" Session is stored on the server, and for small amounts of data such as what you suggest, it scales very well.

Categories

Resources