c# – 表单身份验证添加其他信息以及ReturnUrl
作者:互联网
使用表单身份验证,当应用程序需要重定向到登录页面时,是否有一个事件或任何可扩展点,可以让我在重定向到登录页面之前对请求执行其他工作?
我想在查询字符串中发送可能有所不同的其他信息,以便在web.config中的loginUrl节点的链接中静态嵌入它.
编辑:为了澄清,我想在重定向到登录页面之前拦截请求.
例:
<authentication mode="Forms">
<forms loginUrl="http://the/interwebs/login.aspx" timeout="2880"
enableCrossAppRedirects="true" />
</authentication>
在用户被重定向到http://the/interwebs/login.aspx之前,我希望能够打包查询值,这样url可能会像http://the/interwebs/login.aspx?Action=Refresh那样结束
解决方法:
我以前用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>
标签:c,asp-net,forms-authentication,net-4-0 来源: https://codeday.me/bug/20190606/1189652.html