1
0
Fork 0
mirror of https://gitlab.com/sbrl/GalleryShare.git synced 2018-06-12 22:45:16 +00:00

Windows: Detect HttpListener privilege error and inform the user.

This commit is contained in:
Starbeamrainbowlabs 2016-07-18 15:47:07 +01:00
parent 0926851f05
commit 4bb6a4dcc3
2 changed files with 24 additions and 1 deletions

View file

@ -61,7 +61,22 @@ namespace GalleryShare
router.UrlTransformer = GetFullReqestedPath;
Console.WriteLine("done.");
server.Start();
try
{
server.Start();
}
catch(HttpListenerException error)
{
if (error.Message.Contains("denied") && Environment.OSVersion.Platform == PlatformID.Win32NT && !Utilities.IsAdministrator())
{
Console.Error.WriteLine("Error starting HttpListener: {0}", error.Message);
Console.Error.WriteLine("You are using Windows and have attempted to bind to a non-localhost address, and aren't running GalleryShare as an administrator.");
Console.Error.WriteLine("Please restart GalleryShare with administrative privileges in order to bind to this address.");
Console.Error.WriteLine("Please see http://stackoverflow.com/questions/4019466/httplistener-access-denied for more information.");
return;
}
throw;
}
Console.WriteLine("Listening for requests on {0}.", prefix);
Console.WriteLine("Serving from {0}. Browser url: http://localhost:{1}/", servingDirectory, Port);

View file

@ -5,6 +5,7 @@ using System.Reflection;
using System.IO;
using System.Net.Mime;
using System.Net;
using System.Security.Principal;
namespace GalleryShare
{
@ -58,6 +59,13 @@ namespace GalleryShare
/*responseData.Close();
cycle.Response.Close();*/
}
public static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}