brazilianldsjaguar
brazilianldsjaguar

Reputation: 1449

Check if Browser Exists in ASP.NET

I'm trying to write an web application that, if the request comes from a browser, produces a file, but if it comes from another source (say, for example, a Windows service hitting the site to retrieve a response) it would simply return the response generated.

Would this work?

if (Request.Browser == null)
{
    Response.Write(response);
}
else
{
    Response.Write("You're in a browser. Go Away.");
}

Upvotes: 1

Views: 451

Answers (1)

Rob Scott
Rob Scott

Reputation: 217

It depends on how secure/reliable you want it to be. Assuming that you have control over the Windows Service, a simple way would be to have it specify a specific user-agent string. Then a simple check like this would suffice:

if (Request.UserAgent == "MyWindowsService")
{
    Response.Write(response);
}
else
{
    Response.Write("You're in a browser. Go Away.");
}

Upvotes: 2

Related Questions