예제 #1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login"));
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false);

            var    claim       = (loginInfo.ExternalIdentity as ClaimsIdentity).FindFirst("urn:facebook:access_token");
            string accessToken = string.Empty;

            if (claim != null)
            {
                accessToken = claim.Value;
            }

            if (loginInfo.Login.LoginProvider == "Facebook")
            {
                FacebookGraphAPI fga = new FacebookGraphAPI(accessToken, Global.Globals.FacebookGraphAPIBaseUrl, "me", Global.Globals.DefaultFacebookFields);
                FacebookAccount  facebookAccountInfo = await fga.GetUserInfo(fga);
            }

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    Email = loginInfo.Email
                }));
            }
        }
예제 #2
0
        public async Task <IHttpActionResult> getFacebookResponse([FromUri] string code)
        {
            var facebook = new FacebookGraphAPI(code);
            // Get user profile data
            var    data  = facebook.GetObject("me", null);
            string email = data["email"].ToString();


            IdentityResult result = await _repo.UpdateUser(email, code);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult == null)
            {
                return(Ok());
            }

            else
            {
                UserModel userModel = new UserModel();
                userModel.ConfirmPassword = code;
                userModel.Password        = code;
                userModel.UserName        = email;



                result = await _repo.RegisterUser(userModel);

                errorResult = GetErrorResult(result);

                if (errorResult != null)
                {
                    return(errorResult);
                }

                DAOs.User ctx = new DAOs.User();
                ctx.Scores = 0;
                ctx.Email  = userModel.UserName;
                ctx.Name   = "tên";
                GLOBAL.db.Users.Add(ctx);
                GLOBAL.db.SaveChanges();

                return(Ok());
            }
        }