我正在使用UseOpenIdConnectAuthentication对用户进行身份验证。我的应用程序代码在本地运行良好。但是,当我在Azure上运行它时,永远不会触发SecurityTokenValidated事件。因此,代码运行良好,但用户从未通过身份验证。我不确定问题出在我的代码还是在Azure上。这是在Web窗体,Asp.net应用程序(不是Core)中使用的。我使用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上时,它才不起作用。在本地运行时,事件将按以下顺序触发:
但是,在Azure中,只有RedirectToIdentityProvider被触发。
答案 0 :(得分:0)
尝试在Azure上更改应用程序定义的应用程序清单,以将“ oauth2AllowIdTokenImplicitFlow”属性从false设置为true。
2)在您的startup.cs文件中,更改以下内容:
ResponseType = OpenIdConnectResponseType.Code
to
ResponseType = OpenIdConnectResponseType.CodeIdToken
看看是否有帮助。
答案 1 :(得分:0)
将天蓝色门户网站的App Service Action to take when request is not authenticated
中的Authentication/Authorization
从LogIn with Azure Active Directory
更改为Allow Anonymous requests
。如下图所示:
然后SecurityTokenValidated
将被解雇。应用程序服务身份验证发生在您的应用程序外部,因此应用程序中的自定义身份验证代码永远不会运行。当您将其关闭时,它允许您的应用以与本地相同的方式处理身份验证本身。
这是您可以参考的similar问题。