I would like to allow my users to have their own dedicated URL, for example if my URL is www.XYZ.com, my users will have www.NAME.XYZ.com OR www.XYZ.com/NAME.
At the moment you can see their page by going to www.XYZcom/Member.aspx?userID="012345"
Now I would like to somehow map the URL to the currect system.
I have no idea whatwhat I need to do, I have seem many websites that have done this, so I hope that it would be possible for me to do the same in ASP .net.
Thank you in Advance.
Cheers
If your domain name DNS record is pointing to the IP address of the site, you should be able to access the site using anything.XYZ.com.
When you create a users account, you need to asign them a unique subdomain name and then detect this when users get to the site using their domain name (see the code below)
/// <summary>
/// Gets the Current SubDomain
/// </summary>
/// <returns></returns>
public static string GetSubDomain()
{
string subDomain = String.Empty;
if (HttpContext.Current.Request.Url.HostNameType == UriHostNameType.Dns)
{
subDomain = Regex.Replace(HttpContext.Current.Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
}
if (subDomain.Length == 0)
{
subDomain = "www";
}
return subDomain;
}
If you need to do this with IIS and ASP.NET i think this method will work:
Use a DNS-host that will allow you do register wildcard-domains. That way you can register ****.xyz.com*** to point to your server.
Now comes the part you may not like; for this to work on IIS you will need to set up your site as "default web site". That way, all requests will come to your site, even if the subdomain is not in the list of headers for that site. This will not work if your site is hosted on a shared webhost.
Use Marks method to detect what domain the user has typed in.
HI,
You can use URL Rewriting in your application. Using this feature simply you can redirect URLs like : www.XYZ.com/012345 to the page: www.XYZ.com/Member.aspx?userID="012345". You can download URL Rewriter sample from microsoft website. after you add URLRewriter.dll reference to your website. After you add the following lines to your web.config file in your website:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/([a-z]+)-([a-z]+)\.html</LookFor>
<SendTo>~/Contact.aspx?FirstName=$1&LastName=$2</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
The above code redirects(using URLRewriter library) URLs like www.mydomain.com/name-family.html to www.mydomain.com/Contact.aspx?FirstName=name&LastName=family you can change this to suite your needs. Content of LookFor tag are some regular expression for my case, you should write your own regular expression.
You can view the article and download the source code here at MSDN.
Related
I have an APP using restful server. I want to store PDF's, images, etc. in folders on my server. How can I make the folders private on server, yet allow App to access only certain folders depending on their app access.
I have different users in app and security/tokens established, etc. But if they upload an image for their avatar (and now PDF's), they get stored in folders on the server, and I just display with image source=https://blahblah.com/org1/images/user232.jpg.
How can I make that not accessible to outside (like just going to browser), yet make accessible to app if they have correct login privilege's for that organization/user? And then further extend that logic to more sensative PDF's, and other docs uploaded through app. I didn't want to store in SQL since then harder to use simple image display tools and I already have upload and media managers using folders structures.
I can see how to secure if logging onto server through browser (credentials), but can't see how you connect App with that security level and maintain it for the session.
For future readers. Most of the work was done on the restful (ASP.NET) side. I first tried using authorization/Authentication in web.config and having Allow and deny. This allowed a redirect of a user to a login page; however, it didn't do it if they entered an image exactly correct on website.
Found HTTPHandlers (adding in webconfig ) where I could write code that would be executed once the user entered the specific Image address xyz/abc/image.png. I found this a bit feeling like a hack.
So lastly modified my
routes.MapRoute(
name: "staticFileRoute",
url: "publicstor/{*file}",
defaults: new { controller = "Home", action = "HandleStatic" }
And add a function like this to home controller.
[System.Web.Http.HttpGet]
public ActionResult HandleStatic(string file)
{
if (Session["OrgId"] == null) //todo need to add full security check.
{
return View("Login");
}
else //Either coming from app or coming from web interface
{
string mimeType = MimeInfo.GetMimeType(Path.GetExtension(file));
return File(file, mimeType);
}
}
The final bit is on the Xamarin side to now pass security when getting an image. Since just a simple Xamarin.Forms.Image doesn't have a way to pass login info or tokens/authentication I used
https://forums.xamarin.com/discussion/145575/image-from-url-needing-auth
And established an appwide webclient that logged in generally once forcing my restful to go through security validation, then just accessed the images/documents through out my app from that webclient. So far so good. Hopefully there are no holes.
This gives the gist to a future reader.
I wrote an ASP.NET web application. My application created a request with returning URL other e-commerce server. I want to get this.
http://www.stackoverflow.com/question/ask --> http://www.stackoverflow.com
http://localhost/stackoverflow/question/ask --> http://localhost/stackoverflow
I used Request.Url.AbsoluteUri. But it's not OK for typing address by user.
How can this be done?
Look at the server variables collection. That is the source of this raw data that the HttpApplication gets from IIS.
I think the specific string you are looking for can be found with by "http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"]
EDIT
Looking at your question again, this won't work for the "http://localhost/stackoverflow". This is because it doesn't follow the same convention. If you are using the convention that the public site is http://www.domainname.com/ and your development site is http://localhost/domainname, then you could write a function that gets the site name like
public static string GetDomainUrl(){
var servername = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
bool isLocalHost = serverName.ToLowerInvariant().Contains("localhost);
if(isLocalHost){
var domain = serverName.Split(new Char[]{'/'})[1];
return string.Format(#"http://localhost/{0}", domain);
}
return string.Format(#"http://{0}", serverName);
}
Note: I wrote this in the SO textbox, so check it.
In ASP.NET I'd like to create a link which points to a specific Uri and send this link in an email to a user, for instance something like http://www.BlaBla.com/CustomerPortal/Order/9876. I can create the second part of the Uri /CustomerPortal/Order/9876 dynamically in code-behind. My question is: How can I create the base Uri http://www.BlaBla.com without hardcoding it in my application? Basically I want to have something like:
http://localhost:1234/CustomerPortal/Order/9876 (on my development machine)
http://testserver/CustomerPortal/Order/9876 (on an internal test server)
http://www.BlaBla.com/CustomerPortal/Order/9876 (on the production server)
So is there a way to ask the server where the application is running: "Please tell me the base Uri of the application" ? Or any other way?
Thank you in advance!
Something like this:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath.TrimEnd('/')
You have to put a key in config, something somewhere, because when you think about your web application, it's not really tied to a URL. For example:
http://localhost:1234/
http://yourMachineName:1234/
http://yourMachineName.domain.com:1234/
http://127.0.0.1:1234/
These are just a few ways to get to the same site on localhost....which is it? The same problem exists in production, dozens of domains or IPs may point to the same web application, and it uses host headers or maybe nothing to distinguish it. The point is, when outside the context of a request, the site doesn't really know what URL it goes with, could be anything, there's just not a 1:1 relation there.
If you are in the context of a request when sending an email, then take a look at HttpRequest.Url, this is a Uri type, and you can see the available properties here.
You can do something like this:
var host = HttpContext.Current.Url.Host;
//generate your link using the host
What about to place it into the web.config
<configuration>
<appSettings>
<add key="SendingUrlBase" value="http://www.BlaBla.com"/>
Here is the scenario, a user opens up non-secure page from our WebApp, let's call it PageA, in their browser and then clicks a link in there that takes them to a secure instance of PageB. Once in PageB the user can subsequently click a link that takes them back to a secure instance of PageA (which they already viewed and is in OutputCache). I observed that even though PageA is being accessed over a different URL after visiting PageB (the secure one) it's actually pulling the prior cached copy rather making a fresh one. I verified this behavior in a debugging session, and was surprised that ASP.Net used the same OutputCache item for a secure copy of the page.
My question is why is it this way? And how do I tell the ASP.Net OutPutCache to treat access from secure URL as a different/unique item than the non-secure equivalent?
[Background]
We recently switched our Web Sites images over to use Scene7/Akamai for all images. As a result of this we added code to use different Scene7 url's when viewing a given page on a secure connection. This OutputCache issue is not allowing for the logic that outputs the secure url's to execute, and is resulting in ugly browser warnings.
This doesn't answer the question as worded but it may eliminate your need to vary by scheme. If you are hard coding the "http://" for the Scene7 urls you can change them to scheme-relative urls.
<img src="http://site.scene7.com/images/someimage.jpg" />
=>
<img src="//site.scene7.com/images/someimage.jpg" />
That will cause the browser to automatically ask for the resource with the same scheme as the referring page. That's assuming you have an SSL certificate for your scene7 domain of course.
I think that you can do a VaryByCustom="scheme" and add this to your Global.asax.cs file (inlcuding a couple other others that I use as well app version & user):
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.Equals("version", StringComparison.CurrentCultureIgnoreCase))
{
Assembly asm = Assembly.GetExecutingAssembly();
string[] parts = asm.FullName.Split(',');
string version = parts[1].Trim().ToLower();
return version;
}
else if (custom.Equals("user", StringComparison.CurrentCultureIgnoreCase))
{
var user = Membership.Users.CurrentUser;
return null == user ? string.Empty : user.Id.ToString();
}
else if (custom.Equals("scheme", StringComparison.CurrentCultureIgnoreCase))
{
var scheme = context.Request.IsSecureConnection ? "https" : "http";
return scheme;
}
else
return base.GetVaryByCustomString(context, custom);
}
I've never tried it but you might be able to use the Outputcache VaryByHeader property and the "host" header, which specifies the Internet host and port number of the resource being requested.
The question I'd have is why are you redirecting to PageA over secure after from PageB. If its a non-secure page, couldn't you fix the PageB redirect to always redirect to non-secure.
How would I generate a proper URL for an MVC application to be included in an e-mail?
This is for my registration system which is separate from my controller/action. Basically, I want to send an email verification to fire an Action on a Controller. I don't want to hardcode the URL in, I would want something like the Url property on the Views.
In your Controller, the UrlHelper is just called "Url" - so:
void Index() {
string s = this.Url.Action("Index", "Controller");
}
The "this" is unnecessary, but it tells you where this Url variable comes from
I used:
Html.BuildUrlFromExpression<AccountController>(c=>c.Confirm(Model.confirmedGUID.Value))
It is part of the HTMLHelper (I think in the MVC Futures) so you may have to pass an instance of the HTMLHelper to your service layer, not sure. I use this directly in my view which renders to an email. That gives you the absolute URL and then I store the domain (http://www.mysite.com) in the config file and append it before the URL.
You should probably make the URL part of the configuration of your application.
I know you can do stuff with e.g. the Server property on your web application, but the application will never know if its IP or domain name is reachable from the outside as it might be hidden behind a proxy or a load balancer.
If I'm reading the question correctly, you need to controller/action outside the MVC code. If so, you will need to simply configure the URL in Application Configuration or some such place, unless you have access to the controller classes and use reflection to get the names.