An OAuth 1.0a client library for .NET. Its aim is to provide the clients with a simple and straightforward API in order to authenticate against OAuth 1.0a flow and consume protected resources.
To install TheBlueSky.SwiftOAuth
, run the following command in the Package Manager Console:
Install-Package TheBlueSky.SwiftOAuth
Or search for TheBlueSky.SwiftOAuth
in NuGet Package Manager.
This section assumes you are familiar with OAuth 1.0a protocol; if you're not, feel free to read about it before you continue.
- Get your Consumer Key and Consumer Secret from the service provider, and choose your Redirect URL.
- Get the Signing Algorithm that is supported by the service provider.
- Reference
TheBlueSky.SwiftOAuth
library. - Create a class that implements
TheBlueSky.SwiftOAuth.Services.ISigningAlgorithm
interface to implement the service provider Signing Algorithm; for example:
internal sealed class MyHMACSHA1 : ISigningAlgorithm
{
public string SignatureMethod
{
get { return "HMAC-SHA1"; }
}
public byte[] ComputeHash(byte[] buffer, byte[] key)
{
var algorithm = new System.Security.Cryptography.HMACSHA1 { Key = key };
return algorithm.ComputeHash(buffer);
}
}
- Instantiate
OAuth10
; for example:
var oauth = new OAuth10(ConsumerKey, ConsumerSecret, RedirectUrl, new MyHMACSHA1());
- Request a Request Token; for example:
var token = await oauth.GetRequestTokenAsync(new Uri($"{ServiceProviderUrl}/oauth/request_token"), HttpMethod.Post);
- Use the Request Token to send the user to the service provider log in page; for example:
var authorizeUrl = $"{ServiceProviderUrl}/oauth/authorize?oauth_token={token.Token}&oauth_callback={RedirectUrl}";
Process.Start(authorizeUrl);
-
The user will log in. If the user logs in successfully and provided their consent to your app, the service provider will send the user to your Redirect URL where you can get the tokens generated after this step; let's call them
authenticationResult
. -
Request an Access Token using
authenticationResult
; for example:
var token = await oauth.GetAccessTokenAsync(new Uri($"{ServiceProviderUrl}/oauth/access_token"), HttpMethod.Post, authenticationResult);
- Now you have all you need to access a protected resource. To do so, sign the protected resource URL before calling it; for example:
var url = oauth.GetOAuthSignedUrl(new Uri($"{ServiceProviderUrl}/resource?param1={param1}¶m2={param2}"), HttpMethod.Get);
var result = await httpClient.GetAsync(url);
OAuth 1.0a isn't the simplest flow. This library relieves from writing the boilerplate code necessary to comply with the protocol specifications, in order to successfully authenticate your client and authorise them to access the protected resources.
- .NET Framework 4.5.1
- WinRT for Windows 8.1 and Windows Phone 8.1
- Xamarin.Android
- Xamarin.iOS
- Xamarin.Mac
And it can also be referenced from libraries that target .NET Standard 1.2.