OAuth in C # as a client

I was provided with 6 bits of information to access some data from the website:

Now I have looked at the DotNetOpenAuth and OAuth.NET libraries, and although I'm sure they are very capable of doing what I need, I just can't figure out how to use them that way.

Can someone post some sample code on how to use Url (point 1.) in any library (or in any other way that might work just as well)?

Thanks!

+6
c # oauth
source share
3 answers

I also started working with OAuth a month ago and was also confused by all of these libraries. The only thing I understood about these libraries is that they are quite complex (as you found out). Another thing that makes this difficult is that there are not many examples (this was worse in my case because I was trying to implement a Provider, not a client).

I originally wanted to use the latest version of OAuth 2.0, but the only .NET library that implements it is DotNetOpenAuth. This is probably one of the most comprehensive .NET OAuth libraries, but for me it will take too much time (due to ignorance of WCF, MVC, etc.). Since then, I have abandoned OAuth 1.0a because I found these examples for DevDefined OAuth . I do not know about you, but it was easier for me to learn from examples.

It looks like you just want to implement a client, so be sure to check out the Consumer examples. Try compiling examples and ignoring provider examples because you don't need them and this will make you more confused. Be patient. If you're still confused, it might be a good idea to take a look at some of the libraries created for other languages, as they might make it easier to understand the documentation.

+4
source share

For OAuth 2.0:

I found out that the easiest way is to place the authentication page in an HTML window and then catch the returned access_token. You can then use this in a client web browser.

For example, in MonoTouch it will be:

// // Present the authentication page to the user // var authUrl = "http://www.example.com/authenticate"; _borwser.LoadRequest (new NSUrlRequest (new NSUrl (authUrl))); // // The user logged in an we have gotten an access_token // void Success(string access_token) { _web.RemoveFromSuperview(); var url = "http://www.example.com/data?access_token=" + access_token; // FETCH the URL as needed } // // Watch for the login // class Del : UIWebViewDelegate { public override void LoadingFinished (UIWebView webView) { try { var url = webView.Request.Url.AbsoluteString; var ci = url.LastIndexOf ("access_token="); if (ci > 0) { var code = url.Substring (ci + "access_token=".Length); _ui.Success (code); } } catch (Exception error) { Log.Error (error); } } } 
+1
source share

Ok, I know that your last post was a few months ago, but if you were still working on this (or for people like me who would like to see the answer to this question), here is some information about the NullReferenceException you came up with creating OAuth request:

The null reference comes from IServiceLocator , which is used to resolve dependencies. Unless you explicitly pass one to the constructor, it uses the static ServiceLocator.Current property in the Microsoft.Practices.ServiceLocation namespace.

This is one of the many problems with using static methods and global state; you hide such problems from the consumer of your API. Therefore, if you did not specify a default service locator, null returned, resulting in a NullReferenceException .

So, to fix this problem, I hooked up an IServiceLocator implementation that uses StructureMap (one of the many IoC containers available) as a container. Finally, you will need to register instances for two interfaces: ISigningProvider and INonceProvider . Fortunately, the OAuth.Net.Components assembly has several standard implementations, such as GuidNonceProvider and HmacSha1SigningProvider.

The resulting code looks something like this:

 var container = new Container(); container.Configure(a => a.For<INonceProvider>().Use<GuidNonceProvider>()); container.Configure(a => a.For<ISigningProvider>() .Use<HmacSha1SigningProvider>() .Named("signing.provider:HMAC-SHA1")); var locator = new StructureMapAdapter(container); ServiceLocator.SetLocatorProvider(delegate { return locator; }); 

I understand that this is not the final solution to your initial question (I am still working to get it to work on my own), but I hope these are a few more steps. And if you have long abandoned this implementation ... well, a happy encoding anyway!

+1
source share

All Articles