2
Answers

Google oAuth Authentication fails after deployment in IIS Se

Photo of Ravi Yadav

Ravi Yadav

8y
3k
1

c# - Gmail API not working if the asp.net application is hosted in IIS however it is working

in localhost when executed from visual studio

I am using .Net 4.0 with the below code

 
UserCredential credential;
string gFolder = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/MyGoogleStorage");
string gFolder2 = Server.MapPath(AppSetting.Uploadpath.QuotePath);
 
using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, 
FileAccess.Read))
{
try
{
if (stream != null)
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { gmailScope },
userEmail,
CancellationToken.None,
new FileDataStore(gFolder2, false)
).Result;
}
}
catch (Exception E)
{
ExceptionLogging.LogException(GoogleClientSecrets.Load(stream).Secrets.ClientSecret);
}
finally
{
stream.Dispose();
}
}
 
 

The above code is working in localhost when i am execute from visual studio. But the same code is not working if i host the application in IIS. 

Answers (2)

1
Photo of Ravi Yadav
NA 307 248.5k 8y

The above code is working in localhost when i am execute from visual studio. But the same code is not working if i host the application in IIS. Below are my observations,

  1. For the first time google get a consent from the user to give access to gmail. This will open a default browser in the system and loads the google's consent page. Once the user accepts the consent, the above code will create a json file using "FileDataStore" with access and refresh tokens. This whole process works perfectly in localhost.

  2. But same code is not working if hosted in IIS. However if I put the JSON file that has access and refresh token (generated using localhost) then even application hosted in IIS is also working.

With the above observations, my guess is the process where "google's api is trying to open default browser to load the consent screen" is not working when hosted the application in IIS. Do you have any fix for this problem ? Please let me know if i am missing anything. Thanks.

0
Photo of Bikesh Srivastava
78 23.9k 3.6m 8y
What error you are getting?