Skip to content

Commit 7355cd4

Browse files
committed
avoid returning WWW-Authenticate header if challenged by an ajax request
1 parent 4f727e0 commit 7355cd4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/ZNetCS.AspNetCore.Authentication.Basic/BasicAuthenticationHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,16 @@ protected override Task HandleChallengeAsync(AuthenticationProperties context)
164164
{
165165
var realmHeader = new NameValueHeaderValue("realm", $"\"{this.Options.Realm}\"");
166166
this.Response.StatusCode = StatusCodes.Status401Unauthorized;
167-
this.Response.Headers.Append(HeaderNames.WWWAuthenticate, $"{Basic} {realmHeader}");
168167

168+
if (this.Request.Headers.TryGetValue("X-Requested-With", out var value))
169+
{
170+
if (value == "XMLHttpRequest")
171+
{
172+
return Task.CompletedTask;
173+
}
174+
}
175+
176+
this.Response.Headers.Append(HeaderNames.WWWAuthenticate, $"{Basic} {realmHeader}");
169177
return Task.CompletedTask;
170178
}
171179

0 commit comments

Comments
 (0)