0

I have an Generic Handler where I would like to auto-login based on some querystrings.

But then I set FormsAuthentication.SetAuthCookie(user.Name, false), but HttpContext.Current.User.Identity.IsAuthenticated return false, and I can't redirect because of the limits set in web.config.

So how do I set FormsAuthentications in an .ashx-file?

Casper
  • 373
  • 2
  • 5
  • 19

2 Answers2

1

To perform a login using the FormsAuthentication module, you may want to just use the RedirectFromLoginPage static method, which, under the covers:

  1. prepares the authentication token;
  2. encrypts it;
  3. adds it to the cookie collection of the response;
  4. performs the redirect to the required page (or the default one, as per your web.config).

Here is a short prototype for your handler:

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

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

    FormsAuthentication.RedirectFromLoginPage(username, true);
}

If you are not comfortable by the way this method performs its job, you may do each activity in a manual way:

  1. prepare a FormsAuthenticationTicket with the user name;
  2. encrypt it by way of the Encrypt method;
  3. add it to the response Cookies;
  4. issue a redirect.
Efran Cobisi
  • 6,138
  • 22
  • 22
0

Have you tried adding it as a location path in the 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>
John Tolar
  • 327
  • 1
  • 7