I'm working on a web application and instead of using Membership I use the FormsAuthentication directly.
The problem is when I do a Form post I'm able to get all of the fields but it is not parsing the query strings.
When I look in the debugger the query string is there in the URI but not in the QueryString array.
I'm trying to get the ReturnUrl from the query string so I can make the login page go to the correct spot.
You don't need to parse that QueryString; try just call in your login page:
if(Request.Params["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie);
}
Related
I am unable to get the querystring parameter from url in asp.net
url is:here
My code is
string code = Request.QueryString["code"];
string clientid = Request.QueryString["client_id"];
i am getting code parameter but client_id is null. wt i am doing wrong here.
you are use like,
string code = Request.QueryString["response_type"];
string clientid = Request.QueryString["client_id"];
you are mistake in string code = Request.QueryString["response_type"];
check with this please.
The redirection url is not proper encoded hence during redirection from google the query string parameter after first & in redirect_uri will be ignored (redirect_uri should not contain & as it is you have to keep it in encoded form %26).
The initial url is (contain 'client_id'):
https://accounts.google.com/o/oauth2/auth?scope=profile%20email&state=%2Fprofile&redirect_uri=http://localhost:14772/WebForm2.aspx&response_type=code&client_id=41866849709-lobbqne5v4asm7dn2fu1v8lubm6pc7e7.apps.googleusercontent.com&approval_prompt=force
But after redirected by google its not have Client_Id.
http://localhost:14772/WebForm2.aspx?state=/profile&code=4/gh9Yl3D5Nn220V3nU8d8E5vzC4SmEP6JncnGa6MrgSs#
The issue is redirect_uri content is not encoded correctly. Please configur correctly. It should look like:
http%3A%2F%2Flocalhost%3A14772%2FWebForm2.aspx%26response_type%3Dcode%26client_id%3D41866849709-lobbqne5v4asm7dn2fu1v8lubm6pc7e7.apps.googleusercontent.com%26approval_prompt%3Dforce
I have a url with querystring http://www.sample.com?q=asdasdsdasd . Will it be possible to modify the querystring so that I could replace it with /myaccount i.e at the end the url will look like http://www.sample.com/myaccount.
string destUrl = string.Format("{0}://{1}{2}/",Request.Url.Scheme,Request.Url.Authority,Request.Url.AbsolutePath);
if (destUrl.EndsWith("/"))
destUrl = destUrl.TrimEnd(new char[] { '/' });
if (!string.IsNullOrEmpty(Request.QueryString["paramName"])) {
destUrl = string.Format("{0}?paramName={1}", destUrl, "paramValueHere");
Response.Redirect(destUrl);
Check out url rewriting. You may not be able to achieve the /myaccount direct, but you can tidy up your urls, make them more readable and meaningful for SEO.
You will be able to use to allow your url to look similar to the following :
www.sample.com/account/asdaasdasd
If you lose the query string all together you won't be able to access it at all. Unless you implemented some form of interim code that will get the query string, store it in a session and then redirect to your /myaccount url and get it back there.
I think you are referring to URL Rewriting.
This is quite a commonly used blog post regarding URL rewriting:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Or if you have IIS7, its now been made easier:
http://www.iis.net/download/urlrewrite
In terms of changing ?q=asdasdsdasd to /myaccount though, I don't understand. The first URL seems like a typical search query, and the second is a URL which would probably use cookies etc to pick up the variables (as its for a user account?).
But URL Rewriting can be used so that if you have a user profile with a URL like:
www.sample.com?userprofile.aspx?user=johnsmith
This can be rewritten, using the johnsmith part as a variable like:
www.sample.com/user/johnsmith
With simple string manipulation you could do it as:
string urlWithQuerystring = "http://www.sample.com?q=asdasdsdasd";
int queryStringPos = urlWithQuerystring.IndexOf("?");
string newUrl = String.Format("{0}/myaccount/", urlWithQuerystring.Substring(0, queryStringPos));
Use the this Code in your Global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
string[] parts = Request.RawUrl.Split(new char[]{'/'});
if(Part[1] == "myaccount"))
Context.RewritePath("http://www.sample.com?q=" + Part[2]);
}
and then use this address
http://www.sample.com/myaccount/asdasdasd
I am using ASP.NET.
There is a system that needs to POST data to my site and all they asked for is for me to provide them with a URL.
So I gave them my URL http://www.example.com/Test.aspx.
Now I do not know exactly how they POST it but now on my Test.aspx page I need to write code that will save that data to a database.
But how would this work and what must I do on my Test.aspx page?
I wrote some code in my Page Load Event that sends me an email on Page Load to see if they actually hit the page and it does not seem like they are even?
The data from the request (content, inputs, files, querystring values) is all on this object HttpContext.Current.Request
To read the posted content
StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();
To navigate through the all inputs
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
string value = HttpContext.Current.Request.Form[key];
}
You can get a form value posted to a page using code similiar to this (C#) -
string formValue;
if (!string.IsNullOrEmpty(Request.Form["txtFormValue"]))
{
formValue= Request.Form["txtFormValue"];
}
or this (VB)
Dim formValue As String
If Not String.IsNullOrEmpty(Request.Form("txtFormValue")) Then
formValue = Request.Form("txtFormValue")
End If
Once you have the values you need you can then construct a SQL statement and and write the data to a database.
You need to examine (put a breakpoint on / Quick Watch) the Request object in the Page_Load method of your Test.aspx.cs file.
When someone logs on to my site. I want to direct them to their own home page. If the user has the id of 1. They would go to
http://www.test.com/Home.aspx?id=1
I already have the login and id setup. I am not sure how to incorporate it into the url.
Response.Redirect("http://www.test.com/Home.aspx?id=" + id);
Are you using Forms Authentication?
If so, instead of using RedirectFromLoginPage (which will redirect to whatever page is in your web.config), just use FormsAuthentication.SetAuthCookie, and do your own redirection.
To do so, you need to make use of the URL QueryString.
E.g
// forms auth code here, user is logged in.
int id = 1;
string redirectUrlFormat = "http://www.test.com/Home.aspx{0}";
string queryStringidFormat = "?id={0}";
Response.Redirect(string.Format(redirectUrlFormat, string.Format(queryStringidFormat, id)));
You should handle all querystring parameters, URL, etc (ie the above code) in a global static model class.
That way you can just say:
Response.Redirect(SomeStaticClass.GetUserHomePageUrl(id));
In the receiving page (Home.aspx), use the following code to get the id of the user:
var userId = Request.QueryString["id"]; // again, this "magic string" should be in a static class.
Hope that helps.
How can I redirect www.mysite.com/picture/12345 to www.mysite.com/picture/some-picture-title/12345? Right now, "/picture/12345" is rewritten on picture.aspx?picid=12345 and same for second form of url (picture/picture-title/12323 to picture.aspx?picid12323) I can't just rewrite first form of url to second because i have to fetch picture title from database.
On the first hand, problem looks very easy but having in mind time to parse every request, what would be the right thing to do with it?
Not knowing what is the ASP.NET technology (Webforms or MVC) I will assume it's WebForms.
You can have a look at URL Redirecting and build you own rules. And you do this one time only to apply to all of the links that look like you want.
Scott Guthrie has a very nice post about it.
If what you want is to when it comes to that address redirect to a new one, it's quite easy as well.
First of all let's reuse the code, so you will redirect first to a commum page called, for example, redirectme.aspx
in that page you get the REFERER address using the ServerVariables or passing the Url in a QueryString, it's your chooise and then you can attach the title name, like:
private void Redirect()
{
// get url: www.mysite.com/picture/12345
string refererUrl = Request.ServerVariables["HTTP_REFERER"]; // using the ServerVariables or Request.UrlReferrer.AbsolutePath;
//string refererUrl = Request.QueryString["url"]; // if you are redirecting as Response.Redirect("redirectme.aspx?" + Request.Url.Query);
// split the URL by '/'
string[] url = refererUrl.Split('/');
// get the postID
string topicID = url[url.Length-1];
// get the title from the post
string postTitle = GetPostTitle(topicID);
// redirect to: www.mysite.com/picture/some-picture-title/12345
Response.Redirect(
String.Format("{0}/{1}/{2}",
refererUrl.Substring(0, refererUrl.Length - topicID.Length),
postTitle,
topicID));
}
to save time on the server do this on the first Page event
protected void Page_PreInit(object sender, EventArgs e)
{
Redirect();
}
If you're running IIS7 then (for both webforms and MVC) the URL rewriting module (http://learn.iis.net/page.aspx/460/using-url-rewrite-module/) is worth looking at.
Supports pattern matching and regular expressions for redirects, state whether they're temporary or permanent, plus they're all managable through the console. Why code when you don't have to?
I'm presuming you need a common pattern here and not just a once off solution. i.e. you'll need it to work for 12345 & 12346 & whatever other IDs as well. You're probably looking for URLRedirection and applying a Regular Expression to identify a source URI that will redirect to your Target URI