BasicAuthenticationAttribute now respects the IBasicAuthenticationValidator.Realm when the own Realm property is not set. IBasicAuthenticationValidator.Realm is now a read-only property (removed public set).
This commit is contained in:
@@ -73,9 +73,16 @@ namespace Microsoft.AspNetCore.Authorization
|
||||
|
||||
private void SetAuthenticateRequest(AuthorizationFilterContext context)
|
||||
{
|
||||
var validator = context.HttpContext.RequestServices.GetService<IBasicAuthenticationValidator>();
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user