login asp.net redirect with parameters - c#

I'm writing login page where login is default "Admin" and password is reading from xml file (FileUpload control).
How to redirect to main page, and to know what is path to this file("FileUpload.Name")? Which method of redirecting is appropiate? (sth like redirecting with parameters...but how?

You question is not clear but to redirect the user back to man page after successful login do this:
//I assume a bool variable UserIsValid which you set after validating the user
if (UserIsValid)
{
//If user was redirected back to login page then go back to requested page
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage("User_name", false);
}
else
{
//Set an Auth cookie
FormsAuthentication.SetAuthCookie("User_name", false);
//And then redirect to main page with you parameters if any
Response.Redirect("mainPage.aspx?parameter1={0}&parameter2={1}", param1, param2);
}
}
else
{
//User was not valid, do processing
}

You can get the physical path of a file placed inside your application folder by Server.MapPath
Lets have some examples
[Root_Folder]/FileName.ext
string physicalPath = Server.MapPath("~/FileName.ext");
if file is inside a folder like
[Root_Folder]/App_Data/FileName.ext
string physicalPath = Server.MapPath("~/App_Data/FileName.ext");
physical path will contains like the following string
C:\Websites\MyXYZ\FileName.ext
Now you want to redirect to Home.aspx
Response.Redirect("~/Home.aspx");
if you want to send any querystring parameter, just append as string preceding ? and separated by &
// append as many as required
Response.Redirect("~/Home.aspx?myParam1=" + param1Variable + "&param1=" + param2Variable);
Why don't you try asp.net built in controls i.e. ASP.NET Login Controls Overview
How to: Create an ASP.NET Login Page
Configuring an ASP.NET Application to Use Membership
Suggestion; don't get weird with asp.net login controls if you find your simple solution for accessing file and redirecting.

Related

Pulling external website URL parameters

Currently I have a MVC Registration page which is implemented in an external website.
My question is I need to now extract the previous website's querystring for a certain parameter and read it into my page to register a user into the designated group.
So the workflow looks as follows :
User hits the external website (example: www.test1.com/default.aspx?Code=123asd) with a code in the querystring
user selects Register Now and gets directed to my page.
How will I be able to read the previous querystring code into my MVC page? I do not have access to that website.
EDIT
I currently have a foreach statement which reads the current querystring but this is not pulling the previous request's querystring:
private static string getCode()
{
string nothing = null;
string[] queryStringParaArray = HttpContext.Current.Request.UrlReferrer.Query.Substring(1).Split('=');
if (queryStringParaArray.Length > 0)
{
foreach (var para in queryStringParaArray)
{
if (para.Contains("Code"))
return queryStringParaArray[2];
}
}
return nothing;
}
Have you tried HttpRequest.UrlReferrer property which contains URL of the client's previous request that linked to the current URL. This returns a Uri object and thus you can access the Query property to get the query string value of it.
HttpContext.Request.UrlReferrer
**SideNote: As already commented, there is no guarantee that the referred URL would be present and thus it could be null as well. So would suggest you to pass that query string information directly to your MVC controller while clicking on Register button.

ASP.Net MVC Preventing direct access to action out of RedirectToAction method

My site aims to upload files to sharepoint folders. Users are coming from another system by using a button referencing my site and contains userId and other parameters. When a call comes to my site shown below
http://myapplication/Upload/ToFolder?entityName=new_policy&entityGuid=4451E282-80B0-E611-80E8-C4346BACFC18&systemUserId=1ADEC6E1-008C-430D-A163-D7BA7AD75689
I authenticate user with userId and redirect user to http://myapplication/Policy using RedirectToAction method and setting some parameters to static properties. Because of static properties I use my params but I dont want to do this way. I want to detect that when users types directly http://myapplication/Policy link and throw an error like "You should come from this site only via link on other system."
How can I detect this behaviour of users ?
You could set a tempdata value in the first action...
TempData["isRedirect"] = true;
which gets read by the second action
if(TempData["isRedirect"] == null)
{
//Deny access
}
//Set temp data again so that user can refresh
TempData["isRedirect"] = true;

ASP.NET MVC can I add Request filter to track previous page?

I have ASP.NET MVC Project and I have some pages (let's call it Destination Page) that I can access from multiple pages. So I want to track which page redirects to Destination Page so I can return to it again.
I red about Request Filters .. Can I use it in my case?
Thanks in advance :)
Just pass a return URL in the query string. In other words instead of redirecting like:
return RedirectToAction("Destination");
Do:
return RedirectToAction("Destination", new { returnUrl = Request.RawUrl });
Of course, your "Destination" action needs to accept this as a param:
public ActionResult Destination(Foo otherParam, string returnUrl)
Then, when you're done with whatever you're doing in "Destination", redirect back via:
if (!String.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Fallback");
The IsLocalUrl check is to prevent query string tampering, by ensuring that the return URL is actually local (i.e. relative) to your site.
You can get the refering page using Request.UrlReferrer
otherwise save the last url in a session-variable
like
Session["returnUrl"] = Request.RawUrl;
URL Referrer is only populated by an actual client-click (anchor tag, button).
Not when you manually put it in the URL (which is what my JavaScript is doing).
The solution i am doing to have to with is to create a cookie on the whatever.aspx page, and read that cookie from the JavaScript before i redirect again.

(C#-ASP.NET-REDIRECT-LOGIN) how to get the url before redirecting the page? In login method

how to get the url before redirecting the page?
Example: in A.aspx page has a link that redirects to page b.aspx, and that in "pageload" on page b.aspx I need to grab the link from the previous page: A.aspx.
The scenario is:
When the timeout expires I need to redirect the user to the login page again, but I need save the url that it was accessing, because when he login again I need redirect it to the same page
(C#-ASP.NET-REDIRECT-LOGIN) how to get the url before redirecting the page? In login method
Ty.
Edit
***if (Page.Request.RawUrl != null && !Page.Request.RawUrl.ToUpper().Equals("/DEFAULT.ASPX"))
{
cookie.Value = Page.Request.RawUrl.ToString();
cookie.Expires = DateTime.Now.AddSeconds(SessionLengthMinutes * 2);
Response.Cookies.Add(cookie);
}***
I use this!
Save the link in a session object and then redirect the user.
On successful login retreive the value form session object.
Session["PathBeingAccessed"] = "www.xyz.com";
if (Page.Request.UrlReferrer != null)
{
Response.Redirect(Page.Request.UrlReferrer.ToString(),false);
}
On Login:
if(Session["PathBeingAccessed"]!=null)
{
Response.Redirect(Session["PathBeingAccessed"].toString();)
}
Be carrefull it's a big security issue if the UrlReferrer is not on same web site; I think you need to check url before (domain or whaterver else)

Redirect to the last page requested after login

I have one application in which I want a previous requested page after login.
Like if my application session has timed-out and at that time i'm trying to open 'Balance.aspx' - it will then redirect me to the login page & after I login it is redirect to default page, up to this point it works fine, but now I want the previous page requested (balance page) directly after login authenticate?
Could someone please assist?
try below
string strRedirect = Request["ReturnUrl"];
if (strRedirect == null)
strRedirect = #"~/Default.aspx";
Response.Redirect(strRedirect);
Take a look here at Forms Authentication. This will set up a param in the querystring *ReturnURL* which will return the caller to the page after authenticating.
Look for a section which has a diagram, this will illustrate how it works 'Forms Authentication Control Flow'
You can save the last Page with:
Session["LastPage"] = ....
And then you can redirect to the content of the Session.
after authenticating the page write below code:
string redirecturl = Request["ReturnUrl"];
if (redirecturl == null)
redirecturl = #"~/Default.aspx";
Response.Redirect(redirecturl );

Categories