API 上的 Microsoft.Identity.Web 增量同意 302 CORS

问题描述

我正在尝试使用 AuthorizeforScopesAttribute 获得 AJAX 调用提示以获得增量同意,并在 XmlHttpRequest 失败时更改 window.location。我的 AJAX 操作有 AuthorizeforScopesAttribute 并且正在处理 XmlHttpRequest 中的错误,但 Microsoft.Identity.Web 正在返回 HTTP 302 以尝试直接重定向到身份提供者,这会导致 CORS 问题。

据我所知,使用 AuthorizeforScopesAttribute 框架应该返回 HTTP 401,然后我可以从响应头中获取位置以重定向用户。为什么没有发生这种情况?

这是我的操作:

[AuthorizeforScopes(Scopes = new[] { "user.read" })]
public async Task<IActionResult> GetMyPhoto()
{
    IProfilePhotoContentRequest photoReq = graph.Me.Photo.Content.Request();
    HttpRequestMessage photoReqMessage = photoReq.GetHttpRequestMessage();
    // This throws MsalUirequiredException.
    await graph.AuthenticationProvider.AuthenticateRequestAsync(photoReqMessage).ConfigureAwait(false);

这是 XmlHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open("GET","/Home/GetMyPhoto",true);
xhr.responseType = "blob";
xhr.setRequestHeader("x-ReturnUrl","/");
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.onload = function () {
    if (this.status === 200 || this.status === 304) {
        var img = document.querySelector("img.profilePhoto");
        if (img instanceof HTMLImageElement) {
            img.src = URL.createObjectURL(this.response);
        }
    }
};
xhr.onerror = function () {
    // These are null.
    var allHeaders = this.getAllResponseHeaders();
    var iConsentUri = this.getResponseHeader("Location");

    window.location.href = iConsentUri;
};
xhr.send();

顺便说一下,我像这样调用 Graph 请求的原因是我可以从响应标头中获取 Content-Type。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)