0

我有一个通用处理程序,我想基于某些querystrings自动登录。通用处理程序中的FormsAuthentication.SetAuthCookie ashx

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

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

+0

是否要使用该ashx作为登录方式? – csg 2013-04-03 06:20:35

回答

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
0

您是否尝试将它作为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>