ASP.net 使用 OWIN 进行 Azure Active Directory 联合身份验证服务单点登录

标签 asp.net azure azure-active-directory adfs

我正在开发一个基于此解决方案的项目:https://github.com/Azure-Samples/active-directory-dotnet-webapp-wsfederation

目前,我的用户身份验证方式是默认方式。当页面加载时,我调用我的登录脚本:

Public Sub SignIn()
        If (Not Request.IsAuthenticated) Then
            Try
                Dim newAuth As AuthenticationProperties = New AuthenticationProperties()
                newAuth.RedirectUri = "/"

                HttpContext.Current.GetOwinContext().Authentication.Challenge(newAuth, WsFederationAuthenticationDefaults.AuthenticationType)
            Catch ex As Exception

            End Try

        End If
    End Sub

编辑 要添加更多上下文,这是我的 APP_START/Startup.Auth.vb 代码:

Partial Public Class Startup

        Private realm As String = ConfigurationManager.AppSettings("ida:RPIdentifier")
        Private aadInstance As String = ConfigurationManager.AppSettings("ida:AADInstance")
        Private tenant As String = ConfigurationManager.AppSettings("ida:Tenant")
        Private metadata As String = String.Format("{0}/FederationMetadata/2007-06/FederationMetadata.xml", aadInstance)
        Private authority As String = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant)   


        Public Sub ConfigureAuth(app As IAppBuilder)
            Try
                app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
                app.UseCookieAuthentication(New CookieAuthenticationOptions())
                Dim authOption As WsFederationAuthenticationOptions = New WsFederationAuthenticationOptions()

                app.UseWsFederationAuthentication(New WsFederationAuthenticationOptions() With {
                     .Wtrealm = realm,
                     .MetadataAddress = metadata,                        
                     .Notifications = New WsFederationAuthenticationNotifications() With {
                     .AuthenticationFailed = Function(context)
                                                 context.HandleResponse()
                                                 context.Response.Redirect("Home/Error?message=" + context.Exception.Message)
                                                 Return Task.FromResult(0)
                                             End Function
                                        }
                        })
            Catch ex As Exception
                Throw ex
            End Try


        End Sub


    End Class

不过,我想避免的是,如果我们网络外部的人查看该站点,我不希望他们被重定向到 Azure Single Sign On 登录页面。我只想让他们继续访问网站,我的代码将处理他们可以看到和执行的操作。最终,我将添加一个登录按钮,如果他们不在现场,则可以将他们带到登录页面。但是,现在我如何跳过登录页面?

其次,我想处理 Azure ADFS 关闭的可能性。在这种情况下,我只想将用户作为未经身份验证的用户重定向到该网站。我通过断开互联网连接并运行我的应用程序来测试这一点。我尝试过使用 Try block ,但仍然收到以下错误:

The remote name could not be resolved: 'adfs.myCompany.com'

IOException: Unable to get document from: https://adfs.myCompany.com/FederationMetadata/2007-06/FederationMetadata.xml

[InvalidOperationException: IDX10803: Unable to create to obtain configuration from: 'https://adfs.myCompany.com/FederationMetadata/2007-06/FederationMetadata.xml'.]

这些设置是我应该在 Azure 中进行还是在我的代码中进行?对于这些问题中的任何一个,任何帮助都会很棒。我需要的话,我也可以添加我的 Start.Auth.vb 代码。

谢谢

最佳答案

不幸的是,使用 Microsoft 提供的示例将强制执行自动登录。 话虽这么说,有两种选择:

  1. 选择不同的 authentication scheme
  2. 使用具有 oAuth code flow 的 Azure 应用程序当用户点击登录链接时登录,然后读取用户的个人资料并确定其授权权限。

如果我理解有误,请告诉我。希望这可以帮助!

关于ASP.net 使用 OWIN 进行 Azure Active Directory 联合身份验证服务单点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48031667/

相关文章:

azure - 从 Azure Active Directory 访问所需的响应

Azure - 获取已删除的用户 - 使用 Get-AzureADUser

ASP.NET - 如何仅在尚未包含 CSS 时包含它?

.net - 如何访问iframe父元素?

c# - 选定索引上网格中文本框的 Asp.Net 更改值已更改

azure - 如何使用AAD只允许少数人登录Azure Web App?

javascript - 使用 Javascript API 从 Azure 存储表查询中获取特定列

asp.net - ASP.Net 中的 WF 和并行性需要指导

c# - 我将 ServiceBusTriggerAttribute fork 为 public 并删除了 seal - 出现异常

java - Microsoft Azure 翻译 API 身份验证方法