【问题标题】:Asp.net UseOpenIdConnectAuthentication not working in AzureAsp.net UseOpenIdConnectAuthentication 在 Azure 中不起作用
【发布时间】:2019-08-09 02:08:28
【问题描述】:

我正在使用 UseOpenIdConnectAuthentication 对用户进行身份验证。我的应用程序代码在本地运行良好。但是,当我在 Azure 上运行它时,不会触发 SecurityTokenValidated 事件。因此,代码运行良好,但用户从未经过身份验证。我不确定问题出在我的代码还是 Azure。这是在 Web 表单、Asp.net 应用程序(不是核心)中使用的。我使用 Azure 跟踪功能进行记录。我可以看到只有“RedirectToIdentityProvider”被触发。没有其他事件被调用。这是我的代码:

Startup.Auth.Vb:

 Public Sub ConfigureAuth(app As IAppBuilder)

      Dim clientId As String = ""
      Dim authority As String = ""
      Dim redirectURI As String

      Trace.TraceInformation("Hit Config Auth function")
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
      JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = New Dictionary(Of String, String)

      app.SetDefaultSignInAsAuthenticationType("Cookies")
      app.UseCookieAuthentication(New CookieAuthenticationOptions() With {
                .AuthenticationMode = AuthenticationMode.Active,
                .CookieManager = New SystemWebCookieManager
            })  


      redirectURI = appSettings("ID_Redirect_URI")
      clientId = appSettings("ID_ClientID")
      authority = appSettings("ID_Authority")
      Trace.TraceInformation(redirectURI)
      Trace.TraceInformation(clientId)
      Trace.TraceInformation(authority)

      Trace.TraceInformation("creating OpenIDAuthOptions")
      Dim OpenIdAuthOption = New OpenIdConnectAuthenticationOptions() With {
           .SignInAsAuthenticationType = "Cookies",
           .Authority = authority,
           .RequireHttpsMetadata = False,
           .ClientId = clientId,
           .ResponseType = "id_token",
           .Scope = "openid profile roles",
           .RedirectUri = redirectURI,
           .PostLogoutRedirectUri = redirectURI,
           .Notifications = New OpenIdConnectAuthenticationNotifications() With {
                .AuthenticationFailed = Function(ctx)
                      Trace.TraceInformation("Auth Failed event")
                      Return Task.FromResult(0)
                 End Function,
                 .SecurityTokenReceived = Function(ctx)
                      Trace.TraceInformation("Sec Token Recieved event")
                      Return Task.FromResult(0)
                  End Function,
                  .MessageReceived = Function(ctx)
                      Trace.TraceInformation("Message Recieved event")
                      Return Task.FromResult(0)
                      End Function,
                  .SecurityTokenValidated = Function(ctx)
                     Trace.TraceInformation("Security token validated")                          
                     Return Task.FromResult(0)
                     End Function,
                  .AuthorizationCodeReceived = Function(ctx)
                     Trace.TraceInformation("Auth Code Recieved event")
                     Return Task.FromResult(0)
                     End Function,
                  .RedirectToIdentityProvider = Function(context)
                   Trace.TraceInformation("start of RedirectToIDProvider")
                    Return Task.FromResult(0)
                    End Function
                    }
            }

            Trace.TraceInformation("adding OpenIdAuthOptyions")
            app.UseOpenIdConnectAuthentication(OpenIdAuthOption)
            Trace.TraceInformation("finihsed adding OpenIdAuthOptyions")
        End Sub

正如我上面提到的,这段代码在本地运行良好。它仅在托管在 Azure 上时不起作用。在本地运行时,事件按以下顺序触发:

  1. RedirectToIdentityProvider
  2. 收到消息
  3. 收到安全令牌
  4. 安全令牌已验证

但是,在 Azure 中,只有 RedirectToIdentityProvider 会被触发。

【问题讨论】:

    标签: asp.net azure owin openid openid-connect


    【解决方案1】:

    将 Azure 门户中应用服务 Authentication/Authorization 部分中的 Action to take when request is not authenticatedLogIn with Azure Active Directory 更改为 Allow Anonymous requests。如下图所示:

    然后SecurityTokenValidated 将被解雇。应用服务身份验证发生在您的应用之外,因此您的应用中的自定义身份验证代码永远不会有机会运行。当您关闭它时,它允许您的应用以与本地相同的方式处理身份验证本身。

    这是您可以参考的similar 问题。

    【讨论】:

      【解决方案2】:

      尝试更改 Azure 上应用程序定义的应用程序清单,将“oauth2AllowIdTokenImplicitFlow”属性从 false 设置为 true。

      1. 转到 Azure 门户,
      2. 选择到 Azure Active Directory
      3. 选择应用注册
      4. 选择您的应用。
      5. 点击清单
      6. 找到值 oauth2AllowIdTokenImplicitFlow 并将其值更改为 true
      7. 点击保存

      2) 在您的 startup.cs 文件中,更改以下内容:

      ResponseType = OpenIdConnectResponseType.Code
      to
      ResponseType = OpenIdConnectResponseType.CodeIdToken
      

      看看有没有帮助。

      【讨论】:

        猜你喜欢
        • 2021-04-19
        • 2021-01-25
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 2011-06-10
        相关资源
        最近更新 更多