【问题标题】:FormsAuthentication.SetAuthCookie in Generic Handler ashx通用处理程序 ashx 中的 FormsAuthentication.SetAuthCookie
【发布时间】:2013-03-16 14:10:24
【问题描述】:

我有一个通用处理程序,我想根据一些查询字符串自动登录。

然后我设置了 FormsAuthentication.SetAuthCookie(user.Name, false),但是 HttpContext.Current.User.Identity.IsAuthenticated 返回 false,由于 web.config 中设置的限制,我无法重定向。

那么如何在 .ashx 文件中设置 FormsAuthentications?

【问题讨论】:

  • 你想用那个 ashx 作为登录方式吗?

标签: forms-authentication ashx formsauthentication generic-handler


【解决方案1】:

要使用 FormsAuthentication 模块执行登录,您可能只想使用 RedirectFromLoginPage 静态方法,在幕后:

  1. 准备身份验证令牌;
  2. 对其进行加密;
  3. 将其添加到响应的 cookie 集合中;
  4. 执行重定向到所需页面(或默认页面,根据您的 web.config)。

这是您的处理程序的简短原型:

public void ProcessRequest(HttpContext context)
{
    // TODO: Determine the user identity

    var username = context.Request.QueryString["username"];

    FormsAuthentication.RedirectFromLoginPage(username, true);
}

如果您对该方法执行其工作的方式感到不舒服,您可以手动执行每个活动:

  1. 准备一个带有用户名的FormsAuthenticationTicket
  2. 通过Encrypt方法加密;
  3. 将其添加到response Cookies
  4. 发出redirect

【讨论】:

    【解决方案2】:

    您是否尝试将其添加为 web.config 中的位置路径?

    <configuration>
       <location path="foo.ashx">
          <system.web>
             <authorization>
                <allow users="*"/>
             </authorization>
          </system.web>
       </location>
       <location >
          <system.web>
             <authorization>
                <allow users="?"/>
             </authorization>
          </system.web>
       </location>
    </configuration>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2010-10-12
      • 2012-10-09
      相关资源
      最近更新 更多