57

Viewing Server Variables in Asp.Net Core

 3 years ago
source link: https://www.pmichaels.net/2021/01/02/viewing-server-variables-in-asp-net-core/?utm_campaign=viewing-server-variables-in-asp-net-core
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Viewing Server Variables in Asp.Net Core

In Asp.Net Core, you can view a list of data known as Server Variables. These are available when hosting inside IIS, and they give information about the request call and security.

You can view any one of these by adding some middleware to your Asp.Net Core project:

app.Use(async (context, next) =>
{
var url = context.Features.Get<IServerVariablesFeature>()["URL"];
await next.Invoke();
}

You’ll need to be hosting inside IIS, and the code should go in the routing zone.

There is also a helper method for this, meaning you can do this instead:

app.Use(async (context, next) =>
{
string a = context.GetServerVariable("URL");
await next.Invoke();
}

I then thought, why not just enumerate all the variables. You can see the list here.

I created a list of these:

public static class ServerVariables
{
public static string[] Variables = new[]
{
"UNENCODED_URL",
"URL",
"QUERY_STRING",
"REMOTE_ADDR",
"ALL_HTTP",
"AUTH_USER",
"AUTH_TYPE",
"REMOTE_USER",
"SERVER_ADDR",
"LOCAL_ADDR",
"SERVER_PROTOCOL",
"ALL_RAW",
"REMOTE_HOST",
"SERVER_SOFTWARE",
"HTTP_RAW",
"HTTP_COOKIE"
};
}

And then, in the middleware, just looped through them:

app.Use(async (context, next) =>
{               
await context.Response.WriteAsync("<div>");
foreach (var variable in ServerVariables.Variables)
{
string result = context.GetServerVariable(variable);
await context.Response.WriteAsync($"<p>{variable}:    <b>{result}</b></p><br />");
}
await context.Response.WriteAsync("</div>");
await next.Invoke();
});

Remember that if you run this hosted in Kestrel, you’ll get nothing back.

References

https://docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524602(v=vs.90)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK