microsoft / azure-devops-dotnet-samples

.NET/C# samples for integrating with Azure DevOps Services and Azure DevOps Server
https://docs.microsoft.com/azure/devops/integrate
MIT License
522 stars 519 forks source link

Auth issues using VssConnection #212

Closed Moeahmad94 closed 5 years ago

Moeahmad94 commented 5 years ago

Hi, I'm trying to use your GitClient to update a repository via an Azure Function, but I'm having issues with the Auth aspect of it. I'm trying to use a personalAccessToken and am following the example shown here: https://github.com/Microsoft/vsts-dotnet-samples. Specifically I'm trying to use the following:

Connection = new VssConnection(new Uri(c_CollectionUri), new VssBasicCredential(string.Empty, personalAccessToken)); GitClient = Connection.GetClient();

This keeps failing on the GitClient = ... line. personalAccessToken is my personalAccessToken, and c_CollectionUri is "https://microsoft.visualstudio.com/DefaultCollection".

I also tried the following from https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/client-libraries/samples?view=vsts but still got an error (a different error than the other method though:

Connection = new VssConnection(new Uri(c_CollectionUri), new VssAadCredential());

Am I doing something wrong, or should I be using a different form of authentication for this? This is one of the errors I am getting:

{"Error converting value \"Microsoft.IdentityModel.Claims.ClaimsIdentity;72f988bf-86f1-41af-91ab-2d7cd011db47\moahm@microsoft.com\" to type 'Microsoft.VisualStudio.Services.Identity.IdentityDescriptor'. Path 'authenticatedUser.descriptor', line 1, position 184."}

Thanks, Moe

Moeahmad94 commented 5 years ago

This is the main error I'm getting:

{"Error converting value \"Microsoft.IdentityModel.Claims.ClaimsIdentity;72f988bf-86f1-41af-91ab-2d7cd011db47\moahm@microsoft.com\" to type 'Microsoft.VisualStudio.Services.Identity.IdentityDescriptor'. Path 'authenticatedUser.descriptor', line 1, position 184."}

I updated the original post to reflect this.

danhellem commented 5 years ago

Have you tried connecting directly to the REST Endpoint and not using the client library?

Moeahmad94 commented 5 years ago

I have not. Do you mean for Auth only, or for everything?

corygehr commented 5 years ago

+1 Also having this issue with Azure Functions and the client libraries.

danhellem commented 5 years ago

For everything. I am not sure if we support the client libs for Azure functions. But you can certainly use direct HTTP calls to the REST endpoints.

Please let me know how it goes.

Also, feel free to add a suggestion ticket if this becomes a priority for you

corygehr commented 5 years ago

@danhellem it doesn't work on Functions v1. Client Libraries (beta) don't work on v2 either given their dependencies on Microsoft.Net.Http (2.2.29) and Microsoft.Bcl (1.1.10) which don't have .NET Core equivalents.

If it's an issue with the Functions runtime I'm not sure it would be in scope for the suggestions link you've added above, but deprecating the two dependencies noted above in favor of packages not explicitly targeting the .NET Framework would help tremendously.

corygehr commented 5 years ago

I should note that the workaround as described here does work, but ideally this wouldn't be necessary:

https://developercommunity.visualstudio.com/content/problem/117356/problem-using-vsts-c-client-library-vssconnection.html

Specifically:

  1. Register the type converters for those types by type name only:
    
    TypeDescriptor.AddAttributes(typeof(IdentityDescriptor), new TypeConverterAttribute(typeof(IdentityDescriptorConverter).FullName));

TypeDescriptor.AddAttributes(typeof(SubjectDescriptor), new TypeConverterAttribute(typeof(SubjectDescriptorConverter).FullName));

I added this just before the line where I create the VssConnection object.