diff --git a/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs b/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs index 6600cffe60..081c2d7e15 100644 --- a/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs +++ b/src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs @@ -79,12 +79,13 @@ namespace Microsoft.AspNetCore.Server.HttpSys { if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_ALREADY_EXISTS) { - throw new HttpSysException((int)statusCode, string.Format(Resources.Exception_PrefixAlreadyRegistered, uriPrefix)); + throw new HttpSysException((int)statusCode, Resources.FormatException_PrefixAlreadyRegistered(uriPrefix)); } - else + if (statusCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_ACCESS_DENIED) { - throw new HttpSysException((int)statusCode); + throw new HttpSysException((int)statusCode, Resources.FormatException_AccessDenied(uriPrefix, Environment.UserDomainName + @"\" + Environment.UserName)); } + throw new HttpSysException((int)statusCode); } } diff --git a/src/Servers/HttpSys/src/Resources.resx b/src/Servers/HttpSys/src/Resources.resx index d51faf9fd4..c9fc309110 100644 --- a/src/Servers/HttpSys/src/Resources.resx +++ b/src/Servers/HttpSys/src/Resources.resx @@ -117,6 +117,11 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + The prefix '{0}' is not registered. Please run the following command as Administrator to register this prefix: +netsh http add urlacl url={0} user={1} +See "Preregister URL prefixes on the server" on https://go.microsoft.com/fwlink/?linkid=2127065 for more information. + The destination array is too small. diff --git a/src/Servers/HttpSys/test/FunctionalTests/Listener/ServerTests.cs b/src/Servers/HttpSys/test/FunctionalTests/Listener/ServerTests.cs index f00518a2db..04cc3c6944 100644 --- a/src/Servers/HttpSys/test/FunctionalTests/Listener/ServerTests.cs +++ b/src/Servers/HttpSys/test/FunctionalTests/Listener/ServerTests.cs @@ -9,7 +9,9 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.HttpSys.Internal; using Microsoft.AspNetCore.Testing; +using Microsoft.Extensions.Logging; using Xunit; namespace Microsoft.AspNetCore.Server.HttpSys.Listener @@ -123,6 +125,20 @@ namespace Microsoft.AspNetCore.Server.HttpSys.Listener } } + [ConditionalFact] + [SkipOnHelix("https://github.com/dotnet/aspnetcore/pull/20718#issuecomment-618758634")] + public void Server_RegisterUnavailablePrefix_ThrowsActionableHttpSysException() + { + var options = new HttpSysOptions(); + options.UrlPrefixes.Add(UrlPrefix.Create("http", "example.org", "8080", "")); + var listener = new HttpSysListener(options, new LoggerFactory()); + + var exception = Assert.Throws(() => listener.Start()); + + Assert.Equal((int)UnsafeNclNativeMethods.ErrorCodes.ERROR_ACCESS_DENIED, exception.ErrorCode); + Assert.Contains($@"netsh http add urlacl url=http://example.org:8080/ user={Environment.UserDomainName}\{Environment.UserName}", exception.Message); + } + [ConditionalFact] public async Task Server_HotAddPrefix_Success() {