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
Related
Using ASP.NET MVC, I have the following URL returned from a commeweb payment gateway:
http://localhost/ASP_VPC_3Party_DR?&vpc_AVSResultCode=Unsupported&vpc_AcqAVSRe
spCode=Unsupported&vpc_AcqCSCRespCode=Unsupported&vpc_AcqResponseCode=00&vpc_
Amount=100&vpc_AuthorizeId=000281&vpc_BatchNo=1&vpc_CSCRequestCode=N&vpc_CSCRe
sultCode=Unsupported&vpc_Card=AE&vpc_Command=pay&vpc_Locale=en_AU&vpc_MerchTxn
Ref=123&vpc_Merchant=TESTANDREWK&vpc_Message=Approved&vpc_OrderInfo=VPC+Exam
ple&vpc_ReceiptNo=030821000281&vpc_SecureHash=6EB600780CAA5B1C81BF3AF249E4B85
3&vpc_TransactionNo=281&vpc_TxnResponseCode=0&vpc_Version=1
I need to store this:
&vpc_AVSResultCode=Unsupported&vpc_AcqAVSRe
spCode=Unsupported&vpc_AcqCSCRespCode=Unsupported&vpc_AcqResponseCode=00&vpc_
Amount=100&vpc_AuthorizeId=000281&vpc_BatchNo=1&vpc_CSCRequestCode=N&vpc_CSCRe
sultCode=Unsupported&vpc_Card=AE&vpc_Command=pay&vpc_Locale=en_AU&vpc_MerchTxn
Ref=123&vpc_Merchant=TESTANDREWK&vpc_Message=Approved&vpc_OrderInfo=VPC+Exam
ple&vpc_ReceiptNo=030821000281&vpc_SecureHash=6EB600780CAA5B1C81BF3AF249E4B85
3&vpc_TransactionNo=281&vpc_TxnResponseCode=0&vpc_Version=1
... as a single variable in my database
You ask about full url , but what you need is the query_string.
Full url: Request.Url.AbsoluteUri
Query string: Request.Url.Query
You can get full url.
Request.Url.PathAndQuery
then parse string str.Substring(str.IndexOf('&'))
it gets whole string from starting of &
I am trying to get a value from a Encoded URL in C#. So for example, I am trying to get "customerID" from:
http://<DOMAIN>/default.aspx%3FcustomerID%3D12345%26reference%3D2222
I tried the following:
string customerID = HttpUtility.UrlDecode(Request.QueryString["customerID"]);
But it comes back NULL. What is the proper way to get this value??
Thanks
Jay
string str = " http://DOMAIN/default.aspx%3FcustomerID%3D12345%26reference%3D2222";
var url = HttpUtility.UrlDecode(str);
var parameters = HttpUtility.ParseQueryString(new Uri(url).Query);
var id = parameters["customerID"];
your application should be encoding the ? and = signs. The Request variables are setup by iis usually before the information is handed off to the application handling the request. You need to send the should encode values but not the entire url. If your url looked like,
?customerID=1234 I imagine it would work, the problem is not with your code but with how the url is being constructed.
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
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.
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);
}