Skip to content

Commit

Permalink
Merge pull request #16 from Microsoft/users/aldoms/userAgent
Browse files Browse the repository at this point in the history
Users/aldoms/user agent
  • Loading branch information
aldoms authored Jul 25, 2018
2 parents 66555b3 + 54de0c6 commit 8be3f95
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="packages\MicroBuild.Core.0.3.0\build\MicroBuild.Core.props" />

<PropertyGroup>
<ProductVersion>0.1.1</ProductVersion>
<ProductVersion>0.1.2</ProductVersion>
<ProjectGuid>4966bd57-3289-44a3-9698-f750a35b726b</ProjectGuid>
<SchemaVersion>2.0</SchemaVersion>
<OutputArchitecture>neutral</OutputArchitecture>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use vs

package name=Microsoft.CredentialProvider
version=0.1.1
version=0.1.2

folder InstallDir:\Common7\IDE\CommonExtensions\Microsoft\NuGet\Plugins\CredentialProvider.Microsoft
file source=$(PluginBinPath)\CredentialProvider.Microsoft.exe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ApplicationIcon>helpericons.ico</ApplicationIcon>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>Microsoft.NuGet.CredentialProvider</id>
<version>0.1.1$VersionSuffix$</version>
<version>0.1.2$VersionSuffix$</version>
<title>Microsoft Credential Provider for NuGet</title>
<authors>Microsoft</authors>
<owners>microsoft,nugetvss</owners>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ protected virtual async Task<HttpResponseHeaders> GetResponseHeadersAsync(Uri ur
using (var httpClient = new HttpClient())
using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
{
foreach (var userAgent in Program.UserAgent)
{
httpClient.DefaultRequestHeaders.UserAgent.Add(userAgent);
}


logger.Verbose($"GET {uri}");
using (var response = await httpClient.SendAsync(request, cancellationToken))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public async Task<string> CreateSessionTokenAsync(DateTime validTo, Cancellation
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);

foreach (var userAgent in Program.UserAgent)
{
httpClient.DefaultRequestHeaders.UserAgent.Add(userAgent);
}

var content = new StringContent(
JsonConvert.SerializeObject(
new VstsSessionToken()
Expand Down
35 changes: 32 additions & 3 deletions CredentialProvider.Microsoft/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -22,6 +23,35 @@ namespace NuGetCredentialProvider
{
public static class Program
{
internal static string Name => name.Value;
internal static string Version => version.Value;

internal static IList<ProductInfoHeaderValue> UserAgent
{
get
{
return new List<ProductInfoHeaderValue>()
{
new ProductInfoHeaderValue(Name, Version),
#if NETFRAMEWORK
new ProductInfoHeaderValue("(netfx)"),
#else
new ProductInfoHeaderValue("(netcore)"),
#endif
};
}
}

private static Lazy<string> name = new Lazy<string>(() =>
{
return typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product ?? "CredentialProvider.Microsoft";
});

private static Lazy<string> version = new Lazy<string>(() =>
{
return typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "";
});

public static async Task<int> Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -57,13 +87,12 @@ public static async Task<int> Main(string[] args)
{ MessageMethod.SetCredentials, new SetCredentialsRequestHandler(multiLogger) },
};

var version = typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
multiLogger.Verbose(string.Format(Resources.CommandLineArgs, version.InformationalVersion, Environment.CommandLine));
multiLogger.Verbose(string.Format(Resources.CommandLineArgs, Program.Version, Environment.CommandLine));

// Help
if (parsedArgs.Help)
{
Console.WriteLine(string.Format(Resources.CommandLineArgs, version.InformationalVersion, Environment.CommandLine));
Console.WriteLine(string.Format(Resources.CommandLineArgs, Program.Version, Environment.CommandLine));
Console.WriteLine(ArgUsage.GenerateUsageFromTemplate<CredentialProviderArgs>());
Console.WriteLine(
string.Format(
Expand Down

0 comments on commit 8be3f95

Please sign in to comment.