andrey.shedko
andrey.shedko

Reputation: 3238

ASP.NET MVC not work with Forms Authentication

I'm trying to use Facebook for users authorization via Forms Authentication in ASP.NET MVC. I'm getting access token from Facebook and user avatar url and pass these data to Controller, all of this is work just fine until the moment when I need to redirect user. I did tried FormsAuthentication.RedirectFromLoginPage, RedirectToAction, Response.Redict. None of this methods are working as well as no errors. Here is the controller:

        [HttpPost]
        public ActionResult Login(string Url, string AccessToken)
        {
            string username;
            string fullname;
            var client = new Facebook.FacebookClient(AccessToken);
            dynamic result = client.Get("me", new { fields = "username,first_name,last_name" });
            if (result.first_name == null | result.last_name == null)
            {
                username = result.username;
                fullname = null;
            }
            else
            {
                username = result.username;
                fullname = result.first_name + " " + result.last_name;
            }
            if (UserExist(username) == false)
            {
                CreateUser(username, Url, fullname);
                //FormsAuthentication.SetAuthCookie(username, true);
                //return RedirectToAction("Register", "Home");
                FormsAuthentication.RedirectFromLoginPage(username, true);
            }
            else
            {
                HttpCookie c = Request.Cookies.Get("UserGuid");
                if (c == null)
                {
                    c = new HttpCookie("UserGuid")
                    {
                        Value = GetUserGuid(User.Identity.Name),
                        Expires = DateTime.Now.AddYears(1)
                    };
                    Response.Cookies.Add(c);
                }
                if (result.first_name == null || result.last_name == null)
                {
                    username = result.username;
                }
                else
                {
                    username = result.first_name + " " + result.last_name;
                }
                try
                {
                    //FormsAuthentication.SetAuthCookie(username, true);
                    //Response.Redirect(FormsAuthentication.DefaultUrl);
                    FormsAuthentication.RedirectFromLoginPage(username, true);
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }
            return View();
        }

Upvotes: 0

Views: 273

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12334

You can't do a simple redirect when accessing your action method through AJAX. You need to do a redirect in your JS code by providing a response url from your controller action.

You can simply return a redirect url from your controller and do the following in JS:

$.ajax({
  url: url, 
  data: data,
  success: function(resp) {
     window.location.href = resp.Url;
  }
})

And in your controller:

return Json(new {Url = "/Home/Index"});

Upvotes: 1

Related Questions