You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB版ASP.NET Web应用集成Azure AD Auth库遇CORS问题求助

Azure AD身份验证集成到VB ASP.NET Web应用(.NET Framework)时的CORS问题

我正尝试将Microsoft Azure AD Authentication库集成到已有的VB ASP.NET Web Application(.NET Framework)项目中,但在向微软登录服务器发起身份验证挑战时遇到了CORS问题。浏览器控制台报错,代码基于某教程视频转译为VB,在新建测试项目中可正常运行,但现有项目中出现问题。

问题出在AdLogin()子过程中的Page.Request.GetOwinContext.Authentication.Challenge()方法——该方法本该重定向到微软登录验证页面,却触发了CORS错误。我已经确认租户ID、应用ID和授权字符串均正确,尝试过配置多个重定向URI和CORS设置,但都没解决问题,希望得到技术建议。

身份验证启动代码

Public Sub ConfigureAuth(ByVal app As IAppBuilder)

    IdentityModelEventSource.ShowPII = True
    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3

    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
    app.UseCookieAuthentication(New CookieAuthenticationOptions())

    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll)

    app.UseOpenIdConnectAuthentication(New OpenIdConnectAuthenticationOptions With {
        .ClientId = appID,
        .Authority = authority,
        .RedirectUri = redirectUri,
        .PostLogoutRedirectUri = redirectUri,
        .TokenValidationParameters = New TokenValidationParameters() With {
            .ValidateIssuer = False
        },
        .Notifications = New OpenIdConnectAuthenticationNotifications() With {
            .AuthenticationFailed = AddressOf OnAuthenticationFailedAsync,
            .AuthorizationCodeReceived = AddressOf OnAuthorizationCodeReceivedAsync
        }
    })
    app.UseStageMarker(PipelineStage.Authenticate)
End Sub

登录代码

Private Sub AdLogin()

    Dim authProperties As AuthenticationProperties = New AuthenticationProperties()
    authProperties.RedirectUri = "https://localhost:14699/"
    authProperties.AllowRefresh = False
    authProperties.ExpiresUtc = DateTime.UtcNow.AddMinutes(20)

    Page.Request.GetOwinContext.Authentication.Challenge(
        authProperties,
        OpenIdConnectAuthenticationDefaults.AuthenticationType)

End Sub

内容的提问来源于stack exchange,提问作者activeinfor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 03:16:11