This question is related to http://stackoverflow.com/questions/2185479/asp-net-forms-authentication-without-redirect only I cannot get it to work. MVC 2 changed its script files and they are now gotten as listed here:
http://www.asp.net/ajaxlibrary/CDNAjax4.ashx
Now I'm just shooting in the dark but trial and error has narrowed me down to including
MicrosoftAjax.js
MicrosoftAjaxCore.js
MicrosoftAjaxComponentModel.js
MicrosoftAjaxSerialization.js
MicrosoftAjaxNetwork.js
MicrosoftAjaxWebServices.js
MicrosoftAjaxApplicationServices.debug.js
The problem is I now get the javascript exception:
Microsoft JScript runtime error: Sys.InvalidOperationException: The path to the web service has not been set.
From googling around I've found this path should be "~/ScriptServices_AuthenticationService.asmx" but doing something like:
Sys.Services._AuthenticationService.DefaultWebServicePath = "~/ScriptServices_AuthenticationService.asmx";
doesn't work (I'm assuming it's a private member). So, the question, does anyone know how to get ajax / async FormsAuth working? Is there a definitive guide to what js file various MS ajax services depend on? Finally, if none of the above, is there a way to do FormsAuth on the server side such that it doesn't redirect (that way I can just call the controller action with an ajax form)? ... I tried using FormsAuthentication.SetAuthCookie() but that didn't seem to work for me.
Edit: I figured out that the FormsAuthentication.SetAuthCookie() is actually working but it requires the Response stream to be closed (when, of course, the buffer is sent and the cookies are set). This doesn't work for me since that's too late for my purposes.
Edit: The complete description for what I'm attempting may be necessary. The ajax form I have is for creating a new account. On the server side I create the account and login. On the front end I wait for the ajax completion event and then advance my signup wizard form to the next page that walks through other registration steps. In pseudo code, the controller action looks like:
public JsonResult CreateAsync(LoginForm form)
{
CreateAccount(form);
FormsAuthentication.RedirectFromLoginPage(form.Email, false);
return Json(
new
{
Email = form.Email,
Name = form.Name,
Message = "Success"
});
}
The method for logging in ends the response stream and then the client-side ajax gets confused. I've tried FormsAuth.SetAuthCookie instead but has the issues as described above.