【问题标题】:Forms Authentication adding additional information along with ReturnUrl表单身份验证与 ReturnUrl 一起添加附加信息
【发布时间】:2010-10-12 22:15:38
【问题描述】:

当应用程序需要重定向到登录页面时,使用表单身份验证是否有事件或任何扩展点可以让我在重定向到登录页面之前对请求进行额外的工作?

我想在查询字符串中发送其他信息,这些信息可能会有所不同,因此仅将其静态嵌入到 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 一样结束

【问题讨论】:

  • 您今天如何处理重定向?通过代码或通过 Web.config -> 授权?
  • 使用 web.config 中的 loginUrl 值,这一切都必须在自定义 http 模块中完成吗?
  • 您可能需要将代码放在 Begin_Request (Global.asax) 中,以确定此请求是否是由于对安全页面的请求而导致的登录页面(检查 url 和 QS),然后在QS中附加您的值。顺便说一句 - 您想发送什么“附加信息”,也许我们可以想到一个解决方法。
  • 一个是action消息,最正常的值是SignIn,我还需要返回url是绝对路径

标签: c# asp.net .net-4.0 forms-authentication


【解决方案1】:

您可以通过处理事件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
        }

仅供参考,我们目前通过 IHttpModule 实现来做到这一点。

【讨论】:

  • 这不能回答问题
【解决方案2】:

我以前用 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>

【讨论】:

  • 看到我最近在反射器中破解了打开的 FormsAuthentication,这段代码似乎是处理这个问题的最准确的解决方案。
  • 当它是确切的答案时,我真的不敢相信有人 DV 发布了这篇文章。
  • 我也很惊讶 - 如果反对者发表评论会很好。
  • @ChrisMarisic:当我复制 ScottE 的代码时,我无法在登录前捕获请求,我总是得到代码 200302 。您能否发布您最终用于您的用例的代码?
【解决方案3】:

您不必使用表单身份验证机制将人们重定向到 URL。你可以在代码中做到这一点。只需sign them in 并使用Response.Redirect 自己重定向它们。

【讨论】:

  • 他不是在谈论 RedirectFromLoginPage,他是在谈论重定向到登录页面的内置代码(ASP.NET 自动处理,作为 web.config 设置的一部分)
  • 是的 RPM 是正确的,这就是我的问题,我需要更改为登录页面提供的内容,反之亦然。
猜你喜欢
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多