Redirecting to www - asp.net

I've added a global.asax to my web project, and I'm trying to check if the domain in the Begin_Request contains "www", and if it doesn't, then redirect them to the page they were requesting, but with the "www" pre-pended to it. For an example, If I try to access : SampleSite.com, it directs me to SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/index.aspx
Here is the code:
<script runat="server">
Sub Begin_Request(ByVal sender As Object, ByVal e As EventArgs) Handles Me.BeginRequest
Dim domain As String = Request.Url.Host
If (Not domain.StartsWith("www")) Then
Dim path As String = ""
path = String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7))
Response.Redirect(path)
End If
End Sub
</script>
Any ideas?

I don't think you should do this by code but using IIS redirect features. Here you have an example about how to do it on IIS 7
In case you still want to stay with this approach, change
String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7));
by
String.Format("{0}://www.{1}", Request.Url.Scheme, Request.Url.AbsoluteUri.Substring(7));

Related

Make asp.net webpage respond to WebClient request

I have run into a problem in my asp.net and vb.net programming. I have made a website which connects to a database to authenticate users. I have made another page which my windows form application uses to authenticate users so the program does not have to connect directly to the MySql Database as this is insecure.
So far, the code for my asp.net page is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString.Keys(0) = "key" And Request.QueryString.Keys(1) = "getapikeyv1" And Request.QueryString.Keys(2) = "authapienc2" Then
Dim key As String = Request.QueryString.Get("key")
If key = "dua9r3rqpK{WPDWp9r93af03r8yqrEPIF{A}W~DW)D08q3raewdawdug9q8wdyaw9wq" Then
returnLabel.Text = "XYeJay4XVh6amvf1kJ34Y6y6hR0sSokP9oJFsi0Mxl4QB0T86W0iac9o2nRfCmFmsONq6sPz+rdX+/NqFk3inHRGnwnqf0IsQXE/M/SvnvY="
Else
returnLabel.Text = "invk"
End If
Else
returnLabel.Text = "invalidrequest"
End If
End If
End Sub
The default value for 'returnLabel' is INVALIDREQUEST. My api keys defined above are working fine so are not needed to be considered here. However, when I use the following code to communicate with the webpage, it always returns with returnLabel's value as 'INVALIDREQUEST'. Even if I put the returnLabel.Text = "1233" at the beginning of the Me.Load sub, the page still returns INVALIDREQUEST when I communicate with it through my program!
I use this code to request:
Try
Dim APIRequest As New WebClient
APIRequest.QueryString.Add("key", r9j0wqfha0fh0wf0.DecryptString128Bit(APIKey5, dVadaihwdq93ra0w0))
APIRequest.QueryString.Add("getapikeyv1", "")
APIRequest.QueryString.Add("authapienc2", "")
Dim ako39rqwfa As String = APIRequest.DownloadString(EncryptionPageUrl)
MsgBox(ako39rqwfa)
Dim m As Match = Regex.Match(ako39rqwfa, "<span id=""returnLabel"">.*?</span>")
APIKey00 = m.Value.Replace("<span id=""returnLabel"">", "").Replace("</span>", "")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
As you can see, the WebClient I use is fine but the Me.Load sub in the webpage is not responding the request made by the program...
Is there any way I can make the webpage respond to the WebClient.DownloadString() function?
Thanks,
Rodit
(ps. c# or vb code would be appreciated)
Change your asp.net page to check the query string like this, not sure if you can assume they are in a certain order:
If Request.QueryString.Get("key") isnot nothing And
Request.QueryString.Get("getapikeyv1") isnot nothing And
Request.QueryString.Get("authapienc2") isnot nothing Then

Cannot read cookie from another page in different web application on same domain Asp.Net

When I open a page from another web application on same domain I am trying to read the cookie that was created on the original page, But it I cant read the cookie. If I move that page in to the same web application then it works, but thats not what I want.
[Domain: MyCompany.mobi]
[WebApp A] - create cookie and launch page
Protected Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim aCookie As New HttpCookie("TestCookie")
aCookie.Value = "Hello World"
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
Dim script = "window.open('http://MyCompany.mobi/webappB/default.aspx?id=TestCookie')"
'open page in WebsiteB
ScriptManager.RegisterStartupScript(Me, Me.GetType, "OpenPage", script, True)
End Sub
[Domain: MyCompany.mobi]
[WebApp B] - read the cookie and display in label
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim id As String = ""
If Request.QueryString("id") IsNot Nothing Then id = Request.QueryString("id").ToString
If Request.Cookies(id) IsNot Nothing Then
Dim aCookie As HttpCookie = Request.Cookies(id)
Label1.Text = aCookie.Name & " : " & aCookie.Value
Else
Label1.Text = "Cannot read cookie"
End If
End Sub
Cookies are associated with domain names; not the physical servers that are pointed to by DNS.
For security reasons, you cannot read cookies from a different domain name.
You need to pass the information in the URL.

Routing in ASP.Net web app

I am trying to use a single RouteURL to route to different pages dependent on the Route name but when I click on a button within my aspx page the page gets routed back to itself:
Here is what I have in my Global.asax
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
RegisterRoutes(RouteTable.Routes)
End Sub
Private Sub RegisterRoutes(ByVal routes As routecollection)
routes.MapPageRoute("1", "test", "~/default1.aspx")
routes.MapPageRoute("2", "test", "~/default2.aspx")
routes.MapPageRoute("3", "test", "~/default3.aspx")
End Sub
And here is what I have put in my default1.aspx page:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Response.RedirectToRoute("2")
End Sub
Can anyone point me in the right direction please?
You have duplicated your routeUrl values in your mapped routes. What's happening is that it is routing to your second route, as found by name "2", but that route is http://yoursite/test, which, when it then processes the request, is matching to the first route entry, or default1.aspx.
You can't use the same routeUrl (i.e., "test"), for all of your mappings.
Further reading: ASP.NET Routing
An example of how you could change it:
Private Sub RegisterRoutes(ByVal routes As routecollection)
routes.MapPageRoute("2", "test/2", "~/default2.aspx")
routes.MapPageRoute("3", "test/3", "~/default3.aspx")
routes.MapPageRoute("1", "test/{*whatever}", "~/default1.aspx")
End Sub
Note in this example that route "1" is at the bottom. This is because routes are matched top-down, so more restrictive matches should be listed first. In this example yourdomain/test/2 will goto default2.aspx, yourdomain/test/3 will goto default3.aspx, and default1.aspx will essentially be the default, catching yourdomain/test, yourdomain/test/4, yourdomain/test/5, etc.

Using and accessing Webresource

I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.

Add script to page from user control .ASP.NET

how can i add a javascript file to the page head from a user control?
Thanks
use Page.RegisterStartupScript("pranay","javascriptFunction()")
put this thing in you load method of user control
check this out :
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx
Are you using master page in your site? If yes then you should do all such includes in the master page. But then also you might face issue with the path of javascript file. You can include the file in following way:
<script type="text/javascript" language="javascript" src="<%= this.ResolveClientUrl("~/Script/jquery.js") %>"></script>
If you want to do it from User Control. Try the following:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the name, type and url of the client script on the page.
Dim csname As String = "ButtonClickScript"
Dim csurl As String = "~/script_include.js"
Dim cstype As Type = Me.GetType()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the include script is already registered.
If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
End If
End Sub

Resources