微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。