简体   繁体   English

ASP.NET MVC无法与表单身份验证一起使用

[英]ASP.NET MVC not work with Forms Authentication

I'm trying to use Facebook for users authorization via Forms Authentication in ASP.NET MVC. 我正在尝试使用Facebook通过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. 我从Facebook和用户头像网址获取访问令牌,并将这些数据传递到Controller,直到我需要重定向用户之前,所有这些工作都很好。 I did tried FormsAuthentication.RedirectFromLoginPage , RedirectToAction , Response.Redict . 我确实尝试过FormsAuthentication.RedirectFromLoginPageRedirectToActionResponse.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();
        }

You can't do a simple redirect when accessing your action method through AJAX. 通过AJAX访问操作方法时,无法进行简单的重定向。 You need to do a redirect in your JS code by providing a response url from your controller action. 您需要通过提供控制器操作的响应网址来对JS代码进行重定向。

You can simply return a redirect url from your controller and do the following in JS: 您只需从控制器返回一个重定向URL,然后在JS中执行以下操作:

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

And in your controller: 在您的控制器中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM