Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report different user-agent strings for ASP.NET and ASP.NET Core #49

Merged
merged 12 commits into from
Oct 11, 2018

Conversation

laura-rodriguez
Copy link
Collaborator

Resolves #48

Copy link
Contributor

@robertjd robertjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests to assert that the user agent string is being built as we expect it?

@@ -47,7 +47,7 @@ private static AuthenticationBuilder AddCodeFlow(AuthenticationBuilder builder,
oidcOptions.GetClaimsFromUserInfoEndpoint = options.GetClaimsFromUserInfoEndpoint;
oidcOptions.SaveTokens = true;
oidcOptions.UseTokenLifetime = false;
oidcOptions.BackchannelHttpHandler = new UserAgentHandler();
oidcOptions.BackchannelHttpHandler = new UserAgentHandler("aspnet-core", typeof(OktaAuthenticationOptionsExtensions).Assembly.GetName().Version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use okta-aspnetcore

@@ -82,7 +82,7 @@ private static void AddJwtBearerAuthentication(IAppBuilder app, OktaWebApiOption
private static void AddOpenIdConnectAuthentication(IAppBuilder app, OktaMvcOptions options)
{
var issuer = UrlHelper.CreateIssuerUrl(options.OktaDomain, options.AuthorizationServerId);
var httpClient = new HttpClient(new UserAgentHandler());
var httpClient = new HttpClient(new UserAgentHandler("aspnet", typeof(OktaMiddlewareExtensions).Assembly.GetName().Version));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use okta-aspnet

}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("user-agent", UserAgentBuilder.GetUserAgent());
request.Headers.Add("user-agent", _userAgentBuilder.GetUserAgent());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use lazy initialization so we only have to call GetUserAgent() once, not on every request. Should be able to just wrap that call in a Lazy<string>.

- Add unit test for UserAgentHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.Factory.StartNew(() => new HttpResponseMessage(HttpStatusCode.OK), cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically you can use Task.FromResult to return a Task<T>

@nbarbettini
Copy link
Contributor

LGTM! If you can use Task.FromResult instead of StartNew, I'd recommend doing that.

httpRequestMessage.Headers.UserAgent.ToString()
.IndexOf(
ProductInfoHeaderValue.Parse($"{frameworkName}/{version.Major}.{version.Minor}.{version.Build}")
.ToString(), StringComparison.InvariantCultureIgnoreCase).Should().BeGreaterOrEqualTo(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, does this work with StringComparison.OrdinalIgnoreCase too, or only InvariantCulture?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it does work with OrdinalIgnoreCase as well.

Copy link
Contributor

@nbarbettini nbarbettini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@laura-rodriguez laura-rodriguez merged commit ee8fc23 into master Oct 11, 2018
@laura-rodriguez laura-rodriguez deleted the 48-report-diff-user-agent-for-aspnet-projects branch October 11, 2018 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants