Google 发布后无法登录

Google login not working after publish

我使用 VS2015,C#。

我在 Google 登录时遇到问题。从我的调试配置 (localhost) 来看,一切正常。发布到服务器后,google login window 根本打不开。并且不会抛出异常。 这是我的代码:

[AllowAnonymous]
public async Task LoginWithGoogle()
{
    HttpRequest request = System.Web.HttpContext.Current.Request;
    string redirectUri = ConfigurationReaderHelper.GetGoogleRedirectUri();

    try
    {            
        ClientSecrets secrets = new ClientSecrets
        {
            ClientId = "***",
            ClientSecret = "***"
        };

        IEnumerable<string> scopes = new[] { PlusService.Scope.UserinfoEmail, PlusService.Scope.UserinfoProfile };

        GoogleStorageCredentials storage = new GoogleStorageCredentials();

        dsAuthorizationBroker.RedirectUri = redirectUri;
        UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync(secrets,
            scopes, "", CancellationToken.None, storage);
    }
    catch(Exception ex)
    {
        throw ex;
    }            
}


//just getting value from applicationSettings - web.config
            public static string GetGoogleRedirectUri()
            {
    #if DEBUG
                return GetValueFromApplicationSettings("RedirectUriDEBUG");
    #elif PRODUKCIJA
                return GetValueFromApplicationSettings("RedirectUriSERVER");            
    #endif
            }

当然,我在 google 控制台上为开发人员添加了服务器地址到原始 uri 和授权重定向 uri。 (就像我为本地主机所做的一样)。我只是不明白这是怎么回事,为什么登录 windows 没有打开?

编辑:

添加 class dsAuthorizationBroker(在我的第一个 post 中丢失了 - 对此感到抱歉):

namespace Notes
{
    public class dsAuthorizationBroker : GoogleWebAuthorizationBroker
    {
        public static string RedirectUri;

        public static async Task<UserCredential> AuthorizeAsync(
            ClientSecrets clientSecrets,
            IEnumerable<string> scopes,
            string user,
            CancellationToken taskCancellationToken,
            IDataStore dataStore = null)
        {
            var initializer = new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = clientSecrets,
            };
            return await AuthorizeAsyncCore(initializer, scopes, user,
                taskCancellationToken, dataStore).ConfigureAwait(false);
        }

        private static async Task<UserCredential> AuthorizeAsyncCore(
            GoogleAuthorizationCodeFlow.Initializer initializer,
            IEnumerable<string> scopes,
            string user,
            CancellationToken taskCancellationToken,
            IDataStore dataStore)
        {
            initializer.Scopes = scopes;
            initializer.DataStore = dataStore ?? new FileDataStore(Folder);
            var flow = new dsAuthorizationCodeFlow(initializer);
            return await new AuthorizationCodeInstalledApp(flow,
                new LocalServerCodeReceiver())
                .AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);
        }
    }


    public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
    {        
        public dsAuthorizationCodeFlow(Initializer initializer)
            : base(initializer) { }

        public override AuthorizationCodeRequestUrl
                       CreateAuthorizationCodeRequest(string redirectUri)
        {            
            return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);            
        }
    }
}
public static async Task<UserCredential> AuthorizeAsync

此方法已在 GoogleWebAuthorizationBroker 中声明,因此如果您打算让此函数的实现优先于基本实现,则需要使用 new 关键字。

public new static async Task<UserCredential> AuthorizeAsync

这就是为什么我假设您在日志记录之前停止了行

UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync

此时,它正在调用基础实现。

除此之外,我通常倾向于使用DotNetOpenAuth for interacting with Google and there are plenty of simple examples to follow, like here and here.. but if you really want to roll your own using Google Apis only then this is best place to start