Exemplo n.º 1
0
        public ActionResult FacebookCallback(string code)
        {
            var     fb          = new Facebook.FacebookClient();
            dynamic accessToken = "";

            try
            {
                dynamic result = fb.Post("oauth/access_token", new
                {
                    client_id     = ConfigurationManager.AppSettings["AppId"],
                    client_secret = ConfigurationManager.AppSettings["AppSecret"],
                    redirect_uri  = RedirectUri.AbsoluteUri.Replace("http", "https"),
                    code          = code
                });
                accessToken = result.access_token;
            }
            catch (Exception) { }

            // Store the access token in the session for farther use
            Session["AccessToken"] = accessToken;

            // update the facebook client with the access token so
            // we can make requests on behalf of the user
            fb.AccessToken = accessToken;

            // Get the user's information
            dynamic me         = fb.Get("me?fields=first_name,middle_name,last_name,id,email");
            string  email      = me.email;
            string  firstname  = me.first_name;
            string  middlename = me.middle_name;
            string  lastname   = me.last_name;

            SocialLogin socialLogin = new SocialLogin();

            socialLogin.EmailAddress           = email;
            socialLogin.FullName               = firstname + (string.IsNullOrEmpty(lastname) ? "" : " " + lastname);
            socialLogin.IsGoogleLogin          = false;
            socialLogin.FacebookProviderUserId = Convert.ToString(me.id);

            UserDomainLogic userDomainLogic = new UserDomainLogic();
            Message         message         = userDomainLogic.RegisterUserthroughSociallogin(socialLogin);

            if (message.MessageType == Domain.Enums.MessageType.Success)
            {
                return(RedirectToAction("Dashboard", "User"));
            }
            else if (message.MessageType == Domain.Enums.MessageType.NewUser)
            {
                TempData["isFromSocialMedia"] = true;
                return(RedirectToAction("NewRegistrationfromSocialPage", "User", new { sm = true }));
            }
            else
            {
                ErrorBlock(message.MessageText);
                return(View("Login"));
            }

            //// Set the auth cookie
            //FormsAuthentication.SetAuthCookie(email, false);
            //return RedirectToAction("Dashboard", "User");
        }
Exemplo n.º 2
0
        public ActionResult ExternalLoginCallback(string returnUrl)
        {
            string ProviderName = OpenAuth.GetProviderNameFromCurrentRequest();

            if (ProviderName == null || ProviderName == "")
            {
                NameValueCollection nvs = Request.QueryString;
                if (nvs.Count > 0)
                {
                    if (nvs["state"] != null)
                    {
                        NameValueCollection provideritem = HttpUtility.ParseQueryString(nvs["state"]);
                        if (provideritem["__provider__"] != null)
                        {
                            ProviderName = provideritem["__provider__"];
                        }
                    }
                }
            }

            GoogleOAuth2Client.RewriteRequest();

            var redirectUrl = Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl });
            var retUrl      = returnUrl;
            var authResult  = OpenAuth.VerifyAuthentication(redirectUrl);

            if (!authResult.IsSuccessful)
            {
                return(Redirect(Url.Action("Account", "Login")));
            }

            // User has logged in with provider successfully
            // Check if user is already registered locally
            //You can call you user data access method to check and create users based on your model

            var    id      = authResult.ExtraData["id"];
            string email   = authResult.ExtraData["email"];
            var    name    = authResult.ExtraData["name"];
            var    picture = authResult.ExtraData["picture"];

            SocialLogin socialLogin = new SocialLogin();

            socialLogin.EmailAddress              = email;
            socialLogin.FullName                  = name;
            socialLogin.IsGoogleLogin             = true;
            socialLogin.GoogleProviderUserId      = id;
            socialLogin.GoogleUserProfileImageUrl = picture;
            UserDomainLogic userDomainLogic = new UserDomainLogic();
            var             message         = userDomainLogic.RegisterUserthroughSociallogin(socialLogin);

            if (message.MessageType == Domain.Enums.MessageType.Success)
            {
                return(RedirectAfterLogin(returnUrl));
            }
            else if (message.MessageType == Domain.Enums.MessageType.NewUser)
            {
                TempData["isFromSocialMedia"] = true;
                return(RedirectToAction("NewRegistrationfromSocialPage", "User", new { sm = true }));
            }
            else
            {
                ErrorBlock(message.MessageText);
                return(View("Login"));
            }
        }