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

Integrated .NET solution #1

Closed
lightbringor opened this issue Sep 29, 2022 · 10 comments
Closed

Integrated .NET solution #1

lightbringor opened this issue Sep 29, 2022 · 10 comments

Comments

@lightbringor
Copy link

lightbringor commented Sep 29, 2022

Hey,

on your blog you asked for information if someone finds a default way to do this.

I got the digest authentication working for an IoT device on .NET 6 just with this little code:

using System.Net;

var handler = new HttpClientHandler(){
 Credentials = new NetworkCredential("user", "pw")
};
var httpClient = new HttpClient(handler);

var response = await httpClient.GetAsync("http://my.url");
response.EnsureSuccessStatusCode();

Seems the framework does automatically detect the authentication method from the initial 401 response and then performs the hashing stuff and so on.

@CallumHoughton18
Copy link
Owner

Hey,

on your blog you asked for information if someone finds a default way to do this.

I got the digest authentication working for an IoT device on .NET 6 just with this little code:

using System.Net;

var handler = new HttpClientHandler(){
 Credentials = new NetworkCredential("user", "pw")
};
var httpClient = new HttpClient(handler);

var response = await httpClient.GetAsync("http://my.url");
response.EnsureSuccessStatusCode();

Seems the framework does automatically detect the authentication method from the initial 401 response and then performs the hashing stuff and so on.

Thats great! I'll test it with .NET 6 and update the blog post and the projects README.md to point out it out. Should save people the hassle of doing it manually if they're on the latest .NET version :)

@EENZEE
Copy link

EENZEE commented Feb 15, 2023

Any ideas on what do you do with the realm? I have been provided with a realm.

@CallumHoughton18
Copy link
Owner

Any ideas on what do you do with the realm? I have been provided with a realm.

The realm kind of tells you the protection scope of the resource you're accessing (usually it's retrieved from the www-authenticate header`on the initial request). You can use the realm to generate the Digest authentication header required for the second request similar to what's done in HttpClientExtensions.cs. The actual header generation looks like:

            var headerString =
                $"Digest username=\"{digest.Username}\", realm=\"{digest.Realm}\", nonce=\"{digest.Nonce}\", uri=\"{digestUri}\", " +
                $"algorithm=MD5, qop={digest.QualityOfProtection}, nc={digest.NonceCount:00000000}, cnonce=\"{digest.ClientNonce}\", " +
                $"response=\"{digestResponse}\", opaque=\"{digest.Opaque}\"";

@Val9000
Copy link

Val9000 commented Mar 8, 2023

Did anyone of you find a way to work with IHttpClientFactory since this way we have to create a new HttpClient every single time.
Doing it in the startup.cs file is also not an option. Since you sometimes do not know the credentials before compiling.

@CallumHoughton18
Copy link
Owner

Did anyone of you find a way to work with IHttpClientFactory since this way we have to create a new HttpClient every single time.
Doing it in the startup.cs file is also not an option. Since you sometimes do not know the credentials before compiling.

If you’re referring to the way done in this repository you should just be able to use the extension method on the HttpClient instance that’s injected into your service (I think). If you give a code example of what isn’t working for you I’ll try and find the time to take a look :)

@YuriyNev
Copy link

Thank you really works in .NET 6 (probably even works in earlier versions).

@YuriyNev
Copy link

Thank you, @CallumHoughton18, your extension helped a lot at the time!

@CallumHoughton18
Copy link
Owner

Thank you, @CallumHoughton18, your extension helped a lot at the time!

You’re welcome @YuriyNev :)

@rickdgray
Copy link

This worked great! Thanks for the info!

@CallumHoughton18
Copy link
Owner

CallumHoughton18 commented Feb 11, 2024

This worked great! Thanks for the info!

No problem - glad it was helpful :)

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

No branches or pull requests

6 participants