asp.net - 使用 Azure ADFS 和 OWIN 进行 SSO

标签 asp.net vb.net azure single-sign-on owin-middleware

感谢您提供帮助。我有一个可以使用 Active Directory 联合服务进行身份验证以进行单点登录的站点。目前,我的网站的工作方式是,默认情况下,当用户访问我的网站时,我的代码会尝试登录 SSO(我为此使用 OWIN 库)。如果用户不在我们的网络上,则无法进行身份验证,他们将被重定向到我公司的登录页面,他们可以在其中提供他们的公司凭据。

不过,我想改变这种行为。相反,当用户访问我的页面时,如果他们进行了身份验证,则应该正常继续,并且他们应该被重定向到我的网站。但是,如果他们没有进行身份验证,我不希望他们重定向到我们的登录页面。相反,我希望他们被重定向回我的网站,我的代码将决定他们在网站上可以做什么和不能做什么。然后我想提供一个链接,以便他们可以决定转到登录页面。

我想要这种行为,因为该网站的大多数用户不会成为公司网络的一部分,并且无法进行身份验证。所以,默认情况下,他们应该只看到我们的主页。但是,有时公司成员可能在家工作,因此不会在我们的网络上进行自动身份验证。在这种情况下,他们将使用将其发送到 Azure 登录页面的链接。

这是我当前使用的代码(站点是 ASP.net,表单网页(不是 MVC)):

Startup.Auth.vb:

Partial Public Class Startup
    Dim appSettings = ConfigurationManager.AppSettings

    Private realm As String
    Private aadInstance As String
    Private tenant As String
    Private metadata As String
    Private authority As String

    Public Sub ConfigureAuth(app As IAppBuilder)
        Try
            Dim appSettings = ConfigurationManager.AppSettings
            If (appSettings("releaseVersion") = "DEBUG") Then
                realm = ConfigurationManager.AppSettings("test_ida:RPIdentifier")
                aadInstance = ConfigurationManager.AppSettings("test_ida:AADInstance")
                tenant = ConfigurationManager.AppSettings("test_ida:Tenant")
            ElseIf (appSettings("releaseVersion") = "PROD") Then
                realm = ConfigurationManager.AppSettings("ida:RPIdentifier")
                aadInstance = ConfigurationManager.AppSettings("ida:AADInstance")
                tenant = ConfigurationManager.AppSettings("ida:Tenant")
            End If

            metadata = String.Format("{0}/FederationMetadata/2007-06/FederationMetadata.xml", aadInstance)
            authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant)

            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType)
            app.UseCookieAuthentication(New CookieAuthenticationOptions())
            Dim authOption As WsFederationAuthenticationOptions = New WsFederationAuthenticationOptions()

            Dim fn = Function(context)
                         context.HandleResponse()
                         context.Response.Redirect("Home/Error?message=" + context.Exception.Message)
                         Return Task.FromResult(0)
                     End Function

            Dim auth_not As WsFederationAuthenticationNotifications = New WsFederationAuthenticationNotifications() With {
                    .AuthenticationFailed = fn
                 }

            Dim auth_opt As WsFederationAuthenticationOptions = New WsFederationAuthenticationOptions() With {
                 .Wtrealm = realm,
                 .MetadataAddress = metadata,
                 .Notifications = auth_not
               }
            If (Not auth_opt.Wtrealm Is Nothing) Then
                app.UseWsFederationAuthentication(auth_opt)
            Else

            End If

        Catch ex As Exception
            Throw ex
        End Try

    End Sub


End Class

然后,在我的 Default.aspx.vb 页面加载事件中,我执行以下操作:

  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
            Throw ex
        End Try

    End If

问题是,我不知道如何尝试对用户进行身份验证,确定他们是否通过身份验证,并相应地重定向他们。任何帮助将不胜感激。

谢谢

最佳答案

没有可靠/正确的方法来检查匿名用户是否在您的网络内(或者我不知道)。可能的方法是检查您网络内的用户在 Internet 上公开的 IP 地址(范围)。您可以向网络管理员核实这一点。他们可能会告诉您公共(public) IP 地址(范围)。

一旦您知道公共(public) IP 地址(范围),您就可以检查传入请求,以比较该请求是否来自 RedirectToIdentityProvider 函数内的已知 IP 地址范围(范围)。

Dim redirectToIdentityProvider = Function(context)
    Dim isCompanyNetworkUser = companyIPAddress == context.Request.RemoteIpAddress
    ' Or relevant check for range
    ' Dim isCompanyNetworkUser = (companyIPAddressRangeStart < context.Request.RemoteIpAddress AndAlso companyIPAddressRangeEnd > context.Request.RemoteIpAddress

    If Not isCompanyNetworkUser Then
        context.State = NotificationResultState.Skipped
        context.HandleResponse()
    End If

End Function

Dim auth_not As WsFederationAuthenticationNotifications = New WsFederationAuthenticationNotifications() With {
                    .AuthenticationFailed = fn
                    .RedirectToIdentityProvider = redirectToIdentityProvider
    }

您可能需要稍微调整一下,因为我没有尝试过,但可能会为您指明正确的方向。

关于asp.net - 使用 Azure ADFS 和 OWIN 进行 SSO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48174048/

相关文章:

javascript - aspx gridview 启用按钮

javascript - 如何缩小 knockoutjs 代码文件

c# - 没有可用的匹配绑定(bind),并且该类型不可自绑定(bind)。激活 IProductsRepository 时出错

.NET 应对向非托管 API 添加可选参数

azure - 如何将 csv 文件从 Google Cloud Storage 传输到 Azure Datalake Store

azure - 在 kudu 中运行 npm 脚本

c# - 如何使用 asp.net c# 直接打印使用 iTextSharp 动态创建的 PDF?

vb.net - VB.NET 中 MVC3 ViewBag 的后期绑定(bind)问题

c# - WPF:TreeView 或 TreeListView header 水平滚动问题

visual-studio - Visual Studio F5可以将更改发布到azure并打开页面