2

I have already finished MVC site which use Oauth 1.0
When I try to make authorize with google account from localhost (from debug mode) I fluently make authorization without any problem, But when I publish my site on server I have some problem, when I click button "google LogIn" I get error. please see screen below.

Developers please help me to fix this problem. tank you

P.S. 192.168.77.155 -it's my internal server IP, But I can't imagine why to show it.

enter image description here

return Information Hare:

enter image description here

internal class ExternalLoginResult : ActionResult
        {
            public ExternalLoginResult(string provider, string returnUrl)
            {
                Provider = provider;
                ReturnUrl = returnUrl;
            }

            public string Provider { get; private set; }
            public string ReturnUrl { get; private set; }

            public override void ExecuteResult(ControllerContext context)
            {
                OAuthWebSecurity.RequestAuthentication(Provider, ReturnUrl);
            }
        }

public ActionResult ExternalLoginCallback(string returnUrl)
        {
            GooglePlusClient.RewriteRequest();

            var result = OAuthWebSecurity.VerifyAuthentication();
            if (result.IsSuccessful)
            {
                ProfilePicture helper = new ProfilePicture();

                // name of the provider we just used
                OauthProvider provider = helper.GetProvider(result.Provider);
                if ((int)provider == 0)
                {
                    Logger.Fatal("Unknown Oauth Provider try to SignIn. Check Providers Name (maybe it changeed)");
                    return null; //todo MessageBox for Unkown Provider, or something wrong
                }
                // provider's unique ID for the user
                var uniqueUserID = result.ProviderUserId;
                // since we might use multiple identity providers, then 
                // our app uniquely identifies the user by combination of 
                // provider name and provider user id
                var uniqueID = provider + "/" + uniqueUserID;

                // we then log the user into our application
                // we could have done a database lookup for a 
                // more user-friendly username for our app
                FormsAuthentication.SetAuthCookie(uniqueID, false);

                string userName;
                string nameAndLsatName = string.Empty;
                var userDataFromProvider = result.ExtraData;
                if (provider.Equals(OauthProvider.Twitter))
                {
                    userName = result.UserName;
                }
                else
                {
                    userName = userDataFromProvider["username"];
                    nameAndLsatName = userDataFromProvider["name"];
                }

                //Check if user already is in Db with Provider
                var chekUserName = Uow.Users.Data.Where(x => x.UserName == userName && x.UserGroup.Id == (int)provider).FirstOrDefault();
                if (chekUserName == null)
                {
                    MM.Data.Model.User user = new MM.Data.Model.User();

                    user.UserName = userName;
                    if (!provider.Equals(OauthProvider.Twitter))
                    {
                        user.FirstName = nameAndLsatName.Split(' ')[0];
                        user.LastName = nameAndLsatName.Split(' ')[1];
                    }
                    user.Email = userName; //it'a Email
                    if (provider.Equals(OauthProvider.Twitter))
                    {
                        user.ShowNameAndLastName = false;
                    }
                    else
                    {
                        user.ShowNameAndLastName = true;
                    }
                    user.GroupId = (int)provider;
                    if (provider.Equals(OauthProvider.Twitter))
                    {
                        user.ProfilePicture = helper.GetImageInBytesByProvider(provider, userName);
                    }
                    else
                    {
                        user.ProfilePicture = helper.GetImageInBytesByProvider(provider, uniqueUserID);
                    }
                    Uow.Users.Add(user);
                    Uow.SaveChanges();

                }

                //Valid Login
                //todo need improvement
                var userModel = Uow.Users.GetSingle(x => x.UserName == userName && x.UserGroup.Id == (int)provider);
                Session["User"] = new LoggedUserModel
                {
                    Id = userModel.Id,
                    UserName = userName,
                    ProfilePicture = userModel.ProfilePicture
                };

                Session["UserId"] = userModel.Id;

                //FormsAuthentication.SetAuthCookie(useruserNamename, false);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Index", "Home");

                // return View("", result);
            }
            return null; //need change
        }
Avtandil Kavrelishvili
  • 1,651
  • 3
  • 27
  • 37

1 Answers1

0

in the screenshot that you attached, I see that redirect_uri is your 192.168.77.155 ip. If you correct it, google will redirect back to the correct ip address.

Rajat
  • 410
  • 4
  • 19
  • my site do not use my server internal IP nowhere (192.168.77.155) it isn't public IP. – Avtandil Kavrelishvili Mar 29 '16 at 06:18
  • Please post the code you used to redirect to the Google Screen – Rajat Mar 29 '16 at 10:08
  • I think it's not code guilt, because in Localhost everything works well. but I already Update Question and please see it – Avtandil Kavrelishvili Mar 30 '16 at 09:20
  • The code you posted of the method that will be hit on CallBack. I am saying that your code will never reach this method because the redirect_url is having server name. We need to figure out how the redirect_url was constructed. Are you using Startup.Auth to configure the Google Login – Rajat Mar 30 '16 at 09:48
  • I think the problem is not related to code at all. You are either having a load balancer and the ip of the load balancer is picked or the server IIS is not configured properly. The code that you showed over here will always have the relative path(as is written in your code). – Rajat Mar 31 '16 at 07:32
  • thank you very much, I'll check it and I'll write hare final result – Avtandil Kavrelishvili Mar 31 '16 at 07:45