In Startup.cs configure method when setting cookie authentication options the login path in the examples seem to reference a web root path.
https://docs.asp.net/en/latest/security/authentication/cookie.html
What if I am deploying the app in a virtual directory and do not want to hardcode it?
I could add the virtual directory to a config file but it seems like a step backwords. When I had similar issues before it seemed like i could use Url.Action or the VirtualPathUtility. Is there something similar in asp.net core that can be used in startup.cs?
Related
I have a Runbook in Azure that uses AcmeSharp to generate Let's Encrypt certificates for a website running in Azure App Services. I have used it many times successfully on many ASP.Net sites. Apparently I've never tried it on an ASP.Net Core (2.2) site until now.
I'm pretty sure I was running into the problem described in this blog post - https://ronaldwildenberg.com/letsencrypt-for-asp-net-core-on-azure. Basically, the script publishes a static file to /.wellknown/acme-challenge/randomstring/index.html in my site and then Let's Encrypt tries to verify that file. I'm getting a 404 when trying to hit this URL even though I can see it in the file system in Kudu.
I felt like this was a static file issue in ASP.Net Core and when I found the blog post referred to above - I thought that was going to be the answer. I changed my code as prescribed in the article, but I'm still getting the 404.
Slightly different than the article, instead of files with long random strings of characters like in the article screenshot, my script generates a string like that but creates a folder with that name. Inside each folder is one file (named index.html) that contains the validation info Let's Encrypt is looking for. You can see this at http://www.technicality.online/.well-known/acme-challenge/
You can see the folders are browsable and if you click one, you can see the link to index.html. The problem is - if you click index.html, you get a 404. I've put this in my Startup.Configure:
var rootPath = Path.GetFullPath(".");
var acmeChallengePath =
Path.Combine(rootPath, #".well-known\acme-challenge");
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = new PhysicalFileProvider(acmeChallengePath),
RequestPath = new PathString("/.well-known/acme-challenge"),
});
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true
});
(I don't think I need the ServeUnknownFileTypes since my file is index.html, as opposed to the long random string in the blog post, but I don't think this should hurt anything either.)
I thought maybe the issue was that the file didn't contain valid html (just a string of characters), but I put another file that did contain valid html and I get a 404 when clicking that one as well.
Is there some other ASP.Net Core (or Azure App Service) detail I'm missing to make the application serve up the index.html files?
I figured this out and am posting the answer to hopefully keep someone else from making the same mistake I did. The issue wasn't at all what I thought it was, but rather - there are two "wwwroot" folders in an ASP.Net Core Azure App Service hosting environment and I wasn't paying close enough attention.
The file system path where Azure hosts your application is D:\home\site\wwwroot. In a "classic" ASP.Net scenario, your static files go in that folder. In an ASP.Net Core scenario, another wwwroot folder is created underneath that one. My script (written for ASP.Net) was creating the ".well-known\acme-challenge" folder beneath the first one. The standard app.UseStaticFiles() doesn't help with those.
Basically, I had:
-home
--site
---wwwroot (hosting root)
----wwwroot (ASP.Net core static files folder)
----.well-known (this was a sibling of the 2nd wwwroot and needed to be a child)
I needed to change my script to put my static files under the 2nd wwwroot so that the app.UseStaticFiles() would serve those files.
I need to host a single service at multiple url's behind an Azure Application Gateway.
The service will be hosted internally at say http://10.11.12.13:1234 and there will only be a single instance of same.
This needs to be exposed via public URL's like:
- foo.example.com/service1
- bar.example.com/service1 etc.
We can configure Application gateway to rewrite the urls coming in to point to the hosted service. Something like example.com/service1 => 10.11.12.13:1234
The problem is that the path to the swagger file is set in Startup.cs:
options.SwaggerEndpoint("/swagger/api/swagger.json", "My API");
There is no HttpRequest available at this point so I can't read the headers and configure as appropriate, which is what we do with PreSerializeFilters to set the basePath in the returned swagger.json.
Currently when a user navigates to https://foo.example.com/service1/swagger, the swagger UI will attempt to pull swagger.json down from https://foo.example.com/swagger/api/swagger.json which is the wrong location.
It should be pulling from https://foo.example.com/service1/swagger/api/swagger.json.
Is there anything similar to PreSerializeFilters that I can do to set the path to the swagger.json itself?
The problem is that the path to the swagger file is set in Startup.cs:
options.SwaggerEndpoint("/swagger/api/swagger.json", "My API");
Set an relative url instead of an absolute one.
options.SwaggerEndpoint("swagger/api/swagger.json", "My API");
Rather than requiring Web.config, ASP .NET 5 provides a number of options to provide configuration data. Info on this can be found at:
Introducing ASP .NET 5 by Scott Guthrie
How can we store configuration data in new asp.net vnext? (StackOverflow)
ASP.NET vNext Moving Parts: IConfiguration by Louis DeJardin
There's an interesting question in the comments section of ScottGu's article:
The config.json file in the example, how is that protected by the webserver/http server? web.config is protected by IIS, but if any file can be used (which is great), it also comes with the burden that the webserver shouldn't serve the file out if one requests it in a URL. Or are there prefab names to choose from?
Can anyone answer this?
In previous versions of ASP.NET root of the project was also a root for the website. Some mechanisms were created to prevent access to files which should not be accessible to the outside world (like a whitelist of mime types, RequestFilteringModule).
This changed in ASP.NET 5 as a website root is no longer a project root.
Website root folder is a subfolder in your project directory (named wwwroot by default, but can be changed in project.json).
This means that everything outside a website root folder is not accessible to the outside world.
config.json file is outside of wwwroot so it won't be ever handled by any requests.
I know that within Symfony2's configuration, there is no reference to the base url, as there is no request; the application could either run in cli or within a web server, and therefore we cannot rely on request. But still, I have configuration that asks for stylesheets or javascript base url (such as the JQueryHelperBundle, where you can set your jquery local path - being the local url). The thing is, is there a way to dynamically set a base url for the configuration, without having to change it so that:
The application can move from any directory under development, whether www/myproject or www/foo/myproject without having to change the settings
Production would work the same, except that rewriting the base url with apache would be detected (virtualhosting is common, where the baseurl is mapped to the web directory as '/').
Is there a way to get that base url information? Would using the difference between $_SERVER['DOCUMENT_ROOT'] minus the kernel root dir be a way to detect such base url? But what about virtualhost rebasing the url to / on the web directory? Hardcoding the base url completely couples the project to where it stands in development, and moving project around would require to change the base url everytime, which is annoying.
So, is there a way to dynamically detect the base url within Symfony2's configuration, according to the environment, without depending on the request?
I had to do that in a service, so I injected the router service in my own service and then:
$baseUrl = $router->getContext()->getHost();
But I considered it more as an hack that a real fixture of Symfony2 framework. For instance, in Controller you can generate absolute url easily (example from the symfony book):
$router->generate('blog_show', array('slug' => 'my-blog-post'), true);
And in the twig template, you have the {{ url }} function
I hope this help
I am working on a framework where .aspx and .master pages are embedded in an assembly, using VirtualPathProvider to route a url to a specific embedded resource.
Sample url: /_framework.aspx/mypage.aspx (which uses /_framework.aspx/mymaster.master)
_framework.aspx will make IIS6 route the request through ASP.NET framework
everything after the .aspx is treated as a PathInfo in the .NET framework
In Visual Studio 2008 web server, the virtualPath is correctly: /_framework.aspx/mypage.aspx
but in IIS6 the virtualPath is: /_framework.aspx
If I request two files: /_framework.aspx/file1.css and /_framework.aspx/file2.css
the file2 will have the same content as file1.
I suspect that IIS6 considers the file path (_framework.aspx) and caches the file stream which is returned from the assembly, thus treating both urls as the same file.
Temporary solution:
I've implemented a CacheDependency class like this
class ImmediateExpiryCacheDependency : System.Web.Caching.CacheDependency
{
public ImmediateExpiryCacheDependency()
{
base.NotifyDependencyChanged(null, null);
}
}
It now expires the file stream cache, but doens't work with master pages, I guess because it is requested before the cache is expired through NotifyDependencyChanged.
Needed solution:
If I returned null in GetCacheDependency, IIS6 doesn't expire the file immediately. What is the correct way to immediately expire a file or disable the caching entirely. Even better, I would like to correct the way that IIS6 deals with the url, since the caching is actually good, if it would use the full file url.
Through my work in the ASP.NET Development Web Server, I had come to conclude that the correct FilePath would include the PathInfo, but I understand now that the IIS implementation is correct.
I changed my code so that ASP.NET files (aspx, ashx) would have a path such as /_framework/Default.aspx (since these files will be routed without special configuration) with a master page path such as /_framework/Site.master (since this is routed internally in the ASP.NET engine) and with image resources with a path /_framework.ashx/image.gif (since the .ashx will be routed to the ASP.NET engine, from where I will then use a kind of StaticFileHandler).
This way, all pages and resources can reside and remain entirely in the assembly :-)