Asp.net UseOpenIdConnectAuthentication 在 Azure 中不起作用
Asp.net UseOpenIdConnectAuthentication not working in Azure
我正在使用 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 上时不起作用。在本地 运行ning 时,事件按以下顺序触发:
- RedirectToIdentityProvider
- 收到消息
- 已收到安全令牌
- 安全令牌已验证
但是,在 Azure 中,只会触发 RedirectToIdentityProvider。
尝试更改 Azure 上应用程序定义的应用程序清单,将 "oauth2AllowIdTokenImplicitFlow" 属性 从 false 设置为 true。
- 转到 Azure 门户,
- Select 到 Azure Active Directory
- Select 应用注册
- Select 您的应用。
- 点击清单
- 找到值 oauth2AllowIdTokenImplicitFlow 并将其值更改为 true
- 点击保存
2) 在您的 startup.cs 文件中,更改以下内容:
ResponseType = OpenIdConnectResponseType.Code
to
ResponseType = OpenIdConnectResponseType.CodeIdToken
看看是否有帮助。
已将 Azure 门户中应用服务 Authentication/Authorization
部分中的 Action to take when request is not authenticated
从 LogIn with Azure Active Directory
更改为 Allow Anonymous requests
。如下图所示:
然后 SecurityTokenValidated
将被解雇。应用服务授权发生在您的应用之外,因此您应用中的自定义授权代码永远没有机会 运行。当您关闭它时,它允许您的应用程序以与本地相同的方式处理身份验证本身。
这是您可以参考的 similar 问题。
我正在使用 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 上时不起作用。在本地 运行ning 时,事件按以下顺序触发:
- RedirectToIdentityProvider
- 收到消息
- 已收到安全令牌
- 安全令牌已验证
但是,在 Azure 中,只会触发 RedirectToIdentityProvider。
尝试更改 Azure 上应用程序定义的应用程序清单,将 "oauth2AllowIdTokenImplicitFlow" 属性 从 false 设置为 true。
- 转到 Azure 门户,
- Select 到 Azure Active Directory
- Select 应用注册
- Select 您的应用。
- 点击清单
- 找到值 oauth2AllowIdTokenImplicitFlow 并将其值更改为 true
- 点击保存
2) 在您的 startup.cs 文件中,更改以下内容:
ResponseType = OpenIdConnectResponseType.Code
to
ResponseType = OpenIdConnectResponseType.CodeIdToken
看看是否有帮助。
已将 Azure 门户中应用服务 Authentication/Authorization
部分中的 Action to take when request is not authenticated
从 LogIn with Azure Active Directory
更改为 Allow Anonymous requests
。如下图所示:
然后 SecurityTokenValidated
将被解雇。应用服务授权发生在您的应用之外,因此您应用中的自定义授权代码永远没有机会 运行。当您关闭它时,它允许您的应用程序以与本地相同的方式处理身份验证本身。
这是您可以参考的 similar 问题。