diff --git a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs index d3d08ae..603d40e 100644 --- a/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs +++ b/AMWD.Common.AspNetCore/Attributes/BasicAuthenticationAttribute.cs @@ -73,9 +73,16 @@ namespace Microsoft.AspNetCore.Authorization private void SetAuthenticateRequest(AuthorizationFilterContext context) { + var validator = context.HttpContext.RequestServices.GetService(); + string realm = string.IsNullOrWhiteSpace(Realm) + ? string.IsNullOrWhiteSpace(validator?.Realm) + ? null + : validator.Realm + : Realm; + context.HttpContext.Response.Headers["WWW-Authenticate"] = "Basic"; - if (!string.IsNullOrWhiteSpace(Realm)) - context.HttpContext.Response.Headers["WWW-Authenticate"] += $" realm=\"{Realm.Replace("\"", "")}\""; + if (!string.IsNullOrWhiteSpace(realm)) + context.HttpContext.Response.Headers["WWW-Authenticate"] += $" realm=\"{realm.Trim().Replace("\"", "")}\""; context.HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized; context.Result = new StatusCodeResult(StatusCodes.Status401Unauthorized); diff --git a/AMWD.Common.AspNetCore/BasicAuthentication/IBasicAuthenticationValidator.cs b/AMWD.Common.AspNetCore/BasicAuthentication/IBasicAuthenticationValidator.cs index d2d865b..b335966 100644 --- a/AMWD.Common.AspNetCore/BasicAuthentication/IBasicAuthenticationValidator.cs +++ b/AMWD.Common.AspNetCore/BasicAuthentication/IBasicAuthenticationValidator.cs @@ -10,9 +10,9 @@ namespace AMWD.Common.AspNetCore.BasicAuthentication public interface IBasicAuthenticationValidator { /// - /// Gets or sets the realm to use when requesting authentication. + /// Gets the realm to use when requesting authentication. /// - string Realm { get; set; } + string Realm { get; } /// /// Validates a username and password for Basic Authentication.