Asp.net

SSO 與 Azure ADFS 和 OWIN

  • January 25, 2018

感謝您提供幫助。我有一個站點可以通過 Active Directory 聯合服務進行身份驗證以進行單點登錄。目前,我的網站的工作方式是,預設情況下,當使用者訪問我的網站時,我的程式碼會嘗試登錄 SSO(為此我使用 OWIN 庫)。如果使用者不在我們的網路上,則無法進行身份驗證,他們將被重定向到我的公司登錄頁面,在那裡他們可以提供公司憑據。

不過,我想改變這種行為。相反,當使用者點擊我的頁面時,如果他們進行了身份驗證,它應該會繼續正常進行,並且他們應該被重定向到我的網站。但是,如果他們沒有進行身份驗證,我不希望他們重定向到我們的登錄頁面。相反,我希望他們被重定向回我的網站,我的程式碼將確定他們在網站上可以做什麼和不能做什麼。然後我想提供一個連結,以便他們可以決定轉到登錄頁面。

我想要這種行為,因為該站點的大多數使用者不會成為公司網路的一部分,也無法進行身份驗證。所以,預設情況下,他們應該只看到我們的首頁。但是,有時公司成員可能在家工作,因此不會在我們的網路上進行自動身份驗證。在這種情況下,他們將使用將他們發送到 Azure 登錄頁面的連結。

這是我目前正在使用的程式碼(站點是 ASP.net,表單網頁(不是 MVC)):

啟動.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 地址(範圍)。您可以向網路管理員諮詢。他們可能會告訴您公共 IP 地址(範圍)。

知道公共 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
   }

您可能需要稍微調整一下,因為我沒有嘗試過,但可能會為您指明正確的方向。

引用自:https://stackoverflow.com/questions/48174048