我以前用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>
如何你今天處理重定向?通過代碼或通過Web.config - >授權? – Alxandr 2010-10-12 22:19:24
使用web.config中的loginUrl值,這是否都必須在自定義http模塊中完成? – 2010-10-12 22:32:55
您可能需要將代碼放在Begin_Request(Global.asax)中,以確定這個請求是否作爲對安全頁面的請求(檢查url和QS)的登錄頁面的結果,然後將值添加到QS。順便說一下 - 您想發送什麼'附加信息',或許我們可以考慮解決方法。 – RPM1984 2010-10-12 22:37:28