A Uniform Resource Identifier (URI) identifies a resource. A URL, or web address, can give the location of a resource using a hostname and a path. For example, a browser uses a URL to send an HTTP request to the server hosting a web app. section 1.4.1 section 31.1.4
The hostname identifies the server’s internet location through DNS, which maps it to an IP address. The request’s path identifies the resource to interact with. The server processes the request and sends an HTTP response back to the browser. section 1.4.1
Configure a listening URL
ASP.NET Core listens by default on http://localhost:5000. On Windows, set ASPNETCORE_URLS=http://localhost:8000 in a command window, then run the published app in that window to change its listening URL. You can also pass a URL with the --urls command-line argument. With IIS as a reverse proxy, the external hostname is configured in IIS, and IIS forwards requests to an internal URL. section 27.4
Treat a return URL supplied by a user as untrusted. An attacker can put a malicious destination in a login link, causing a vulnerable app to redirect users there after sign-in. Check that the destination is local to your app with Url.IsLocalUrl(), or use LocalRedirect(), which throws if the URL is not local. When local-only redirects are not possible, as in some OpenID Connect flows, preregister the allowed redirect URLs. section 29.4.1
Listing 29.5 Detecting open redirect attacks by checking for local return URLs
[HttpPost]
public async Task<IActionResult> Login(
LoginViewModel model, string returnUrl = null) #1
{
// Verify password, and sign user in
if (Url.IsLocalUrl(returnUrl)) #2
{
return Redirect(returnUrl); #3
}
else
{
return RedirectToPage("Index"); #4
}
}
This login example checks the return URL before redirecting and falls back to the home page when it is not local. section 29.4.1
Resource identifiers can also expose database entity IDs. Always check that the user has permission to access or change the requested resource; an ID in a URL is not itself an authorization check. section 29.5
Well-known URIs are useful for fixed-path handlers. For example, ASP.NET Core middleware can respond to the security.txt path with contact information and pass other requests to the next component. Such a handler can be reused independently of the app’s routing configuration. section 31.1.4
Listing 31.8 A Security.txt handler implemented as middleware
public class SecurityTxtHandler
{
private readonly RequestDelegate _next;
public SecurityTxtHandler(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
var path = context.Request.Path;
if(path.StartsWithSegments("/.well-known/security.txt")) #1
{
context.Response.ContentType = "text/plain"; #2
return context.Response.WriteAsync( #2
"Contact: mailto:security@example.com"); #2
}
return _next.Invoke(context); #3
}
}
The handler checks the request path, writes a plain-text response for security.txt, and passes other requests onward. section 31.1.4








