2010-10-12 115 views
4

一起加入附加信息使用窗體身份驗證時,應用程序需要重定向到登錄頁面,有一個事件或任何擴展點,這將讓我做更多的工作,要求其重定向到正負號前在頁面中?窗體身份驗證與RETURNURL

我想發送的查詢字符串的其他信息,可以改變,使得它不會工作,只是靜態地嵌入在在web.config中loginUrl節點的鏈接。

編輯:爲了澄清,我想攔截請求重定向到登錄頁面之前。

例子:

<authentication mode="Forms"> 
     <forms loginUrl="http://the/interwebs/login.aspx" timeout="2880" 
       enableCrossAppRedirects="true" /> 
</authentication> 

而之前用戶被重定向到http://the/interwebs/login.aspx我希望能夠包中查詢值,所以這個網址就落得像http://the/interwebs/login.aspx?Action=Refresh

+0

如何你今天處理重定向?通過代碼或通過Web.config - >授權? – Alxandr 2010-10-12 22:19:24

+0

使用web.config中的loginUrl值,這是否都必須在自定義http模塊中完成? – 2010-10-12 22:32:55

+0

您可能需要將代碼放在Begin_Request(Global.asax)中,以確定這個請求是否作爲對安全頁面的請求(檢查url和QS)的登錄頁面的結果,然後將值添加到QS。順便說一下 - 您想發送什麼'附加信息',或許我們可以考慮解決方法。 – RPM1984 2010-10-12 22:37:28

回答

1

我以前用http模塊完成了這個。我的例子(剝離),在vb.net。我只是檢查一個401狀態碼並做我自己的重定向。這裏最大的竅門是我必須刪除並添加web.config httpModules以確保我的httpmodule從URL授權模塊中跳入。在我的情況下,我爲本地化重寫了一些url,並且我需要將區域設置ID發送到登錄頁面,因爲URL授權模塊中的正常重定向導致它丟失了。我大量使用Reflector來構建基本路徑(未顯示),因爲這些都是私有方法。

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init 
     AddHandler context.EndRequest, AddressOf Context_EndRequest 
    End Sub 

    Private Sub Context_EndRequest(ByVal sender As Object, ByVal e As EventArgs) 

     If TypeOf sender Is HttpApplication Then 

      Dim hApplication As HttpApplication = DirectCast(sender, HttpApplication) 
      Dim hContext As HttpContext = hApplication.Context 

      If (hContext.Response.StatusCode = &H191) Then 

       hContext.Response.Redirect(String.Format("{0}?ReturnUrl={1}&someparam2={2}", basepath, HttpUtility.UrlEncode(hContext.Request.RawUrl), someparam2)) 

      End If 

     End If 

    End Sub 

的web.config變化

<httpModules> 
     <clear/> 
     <!-- custom --> 
     <add name="SomeHttpModule" type="SomeCompany.SomeProduct.SomeHttpModule"/> 
     <!-- add back defaults, exlude PassportAuthentication, AnonymousIdentification, Profile --> 
     <add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/> 
     <add name="Session" type="System.Web.SessionState.SessionStateModule"/> 
     <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/> 
     <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/> 
     <add name="RoleManager" type="System.Web.Security.RoleManagerModule"/> 
     <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/> 
     <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/> 
    </httpModules> 
+0

鑑於我最近破解了在反射器中打開FormsAuthentication,此代碼似乎是處理此問題的最準確解決方案。 – 2010-10-25 17:05:03

+0

我真的不能相信某人DV'd這篇文章,當它是確切的答案。 – 2010-10-29 18:20:22

+0

我也很驚訝 - 如果downvoter留言,本來會很好。 – ScottE 2010-10-29 18:34:52

-1

你不要的東西」不得不使用Forms身份驗證機制將人員重定向到URL。你可以在代碼中做到這一點。只需sign them in並使用Response.Redirect自己重定向它們。

+1

他不是在談論RedirectFromLoginPage,他在談論內置的代碼登錄頁面重定向(其中ASP.NET自動處理,作爲web.config設置的一部分) – RPM1984 2010-10-12 22:47:36

+0

是的RPM是正確的這是我的問題所屬,我需要改變發生什麼事情餵養登錄頁面,而不是反之亦然。 – 2010-10-12 23:06:32

6

您可以通過通過一個IHttpModule的執行處理事件HttpApplication.AuthenticateRequest

private void Application_AuthenticateRequest(Object source, EventArgs e) 
    { 

     HttpApplication application = (HttpApplication)source; 
     HttpContext context = application.Context; 

     string cookieName = FormsAuthentication.FormsCookieName; 
     HttpCookie authCookie = context.Request.Cookies[cookieName]; 

     if (authCookie == null || string.IsNullOrEmpty(authCookie.Value)) 
     { 
      //... do something 
     } 

僅供參考,我們目前做到這一點做到這一點。

+0

這不回答問題 – 2015-08-05 18:35:19