Impersonation using Service account not working in dot net
See original GitHub issueI have looked into the Service account sample to access public post in google plus account. Similarly I am trying to use the service account which is having domain wide delegation to impersonate a user and scan through the email id in a G-Suite account. When I try to call the Gmail API I am getting unauthorized_client exception. Could you please help me to understand what I am missing here? Details of the google console setting and the code snippet is defined below. • Enabled Domain-Wide Delegation for the service account
• Enable the client to access the API in security settings
• Following is the code and the error message which I got in a console application. `public static void AccessGmailInboxWithServiceAccount() {
String serviceAccountEmail = lstrServiceAccountId;//"SERVICE_ACCOUNT_EMAIL_HERE";
var certificate = new X509Certificate2(p12KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = impersonateUserEmail,
Scopes = new[] { GmailService.Scope.MailGoogleCom }
}.FromCertificate(certificate));
// Create the service.
var gmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Gmail API Service Account Sample Impersonating User",
});
var listRequest = gmailService.Users.Messages.List(impersonateUserEmail);
listRequest.Q = "in:INBOX";
var messages = listRequest.Execute().Messages;
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}`
Issue Analytics
- State:
- Created 5 years ago
- Comments:20 (1 by maintainers)
Top Results From Across the Web
Implement impersonation in an ASP.NET application
This article introduces how to implement impersonation by modifying Web.config and running a particular section of code.
Read more >User Impersonation not working in .NET Core
1 Answer 1 ... If WindowsIdentity.GetCurrent().Name shows the correct user, then your impersonation is likely working correctly. File shares in ...
Read more >iis - Impersonation NOT working - Network Share
I have created a virtual Directory (IIS 7.0), which points to a network share. This virtual directory resides under my web application root....
Read more >Service account authentication in .NET Core. : r/dotnet
What are the best practices for having a console app authenticate as a service account (windows authentication) in .NET Core?
Read more >Impersonation in Windows Service with ASP.NET Core
NET Core in a Windows Service. The problem was that when doing WindowsIdentity.GetCurrent().Name; what was being returned was the account ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@AbilashSekharan I don’t know much about delegation, but have noticed a couple of things that might help:
@PinnuDivya the best documentation on setting up can be found here. domain wide deligation
Just swap out the scopes you are using.