谷歌登录在发布的网站ASP.Net MVC中不起作用

时间:2016-12-22 18:23:05

标签: javascript asp.net-mvc login asp.net-mvc-5 google-login

我真的需要一些帮助。我的google登录在localhost中工作,但在发布的网站上不起作用。我已在Google API控制面板中添加了该网址。

Google面板中添加的网址:https://www.connect.companyname.com

错误:redirect_uri_mismatch

LocalHost:当我点击下面的谷歌图标时,会生成URL并且一切都按预期工作

[编码的工作网址]

         https://accounts.google.com/AccountChooser?continue=https://accounts.google.com/o/oaut
h2/auth?response_type%3Dcode%26client_id%*****CLIENT ID******.apps.googleusercontent.com%26
         redirect_uri%3Dhttp://localhost:5271/Security/LoginSettingsSubmit%26scope%3Dopenid%2Bemail
    %26%26from_login%3D1%26as%3D-1004f53b4f4074a4&ltmpl=embedded&btmpl=authsub&scc=1&oauth=1

[解码后的工作网址]

    https://accounts.google.com/AccountChooser?continue=https://accounts.google.com/o/oauth2
/auth?response_type=code&client_id=***** ClientID***.apps.googleusercontent.com&redirect_uri=http://localhost:5271/Security/LoginSettingsSubmit&scope=openid+email&&from_login=1&as=-6396ecb83b38f7e2&ltmpl=embedded&btmpl=authsub&scc=1&oauth=1

已发布的网站:

[不工作]网址

点击Google图标生成

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=**ClientID**.apps.googleusercontent.com&redirect_uri=https%3a%2f%2fconnect.companyname.com%3a3382%2fSecurity%2fLoginSettingsSubmit&scope=openid%20email&

在LocalHost中正常运行

ASP.net MVC 5中的BackEnd代码

 public static string UrlForGoogleAuth
        {
            get
            {
                UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);

                string host = HttpContext.Current.Request.Url.Host;
                if (host == "localhost")
                {
                    host = HttpContext.Current.Request.Url.Authority;
                }

                string result = "https://" + host + url.Action("LoginSettingsSubmit", "Security");

                return result;
            }
        }

JS

 var ClientID = "****.apps.googleusercontent.com";
        var url = "https://accounts.google.com/o/oauth2/auth?";
        var parameters = "response_type=code&client_id=".concat(ClientID).concat("&redirect_uri=@(HttpUtility.UrlEncode(SimUtils.UrlForGoogleAuth))" + "&scope=openid%20email&");
        var loginUri = url.concat(parameters);
 window.open(loginUri);

后端

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LoginSettingsSubmit(SimSettingsLoginMV userData)
        {
            UserMV authenticatedUser = null;

            userData.Validate(ModelState);

            if (ModelState.IsValid)
            {
                switch (userData.LoginType)
                {

                    case LoginTypeEnum.Google:
                        authenticatedUser = SecurityHelper.AuthenticateUser(userData.GoogleCode, SimUtils.UrlForGoogleAuth, userData.LoginType);
                        break;
                }
            }

0 个答案:

没有答案