nemiro-net / nemiro.oauth

Nemiro.OAuth is a class library for authorization via OAuth protocol in .NET Framework
http://oauth.nemiro.net/
Apache License 2.0
40 stars 79 forks source link
asp-mvc asp-net c-sharp dropbox facebook google instagram login mailru oauth oauth-client odnoklassniki twitter vb-net vk vkontakte windows-forms yandex

Nemiro.OAuth

Nemiro.OAuth is a class library for authorization via OAuth protocol in .NET Framework.

The library provides mechanisms for implementing OAuth clients, and also contains a ready-to-use clients for popular websites.

Nemiro.OAuth is distributed under Apache License Version 2.0.

To install Nemiro.OAuth, run the following command in the Package Manager Console:

PM> Install-Package Nemiro.OAuth

Online Demo

demo-oauth.nemiro.net

Features

Less code, more functionality!

System Requirements

License

Nemiro.OAuth is distributed under Apache License Version 2.0.

How to use

1. Create an application at the OAuth provider site.

2. Use these credentials for registration of an OAuth client in your project.

For example, Facebook:

C#

OAuthManager.RegisterClient
(
  "facebook", 
  "1435890426686808", 
  "c6057dfae399beee9e8dc46a4182e8fd"
);

Visual Basic .NET

OAuthManager.RegisterClient _
(
  "facebook", 
  "1435890426686808", 
  "c6057dfae399beee9e8dc46a4182e8fd"
)

3. Create a page to handle the callback. And add code to obtain user data with external server.

For example:

C#

public partial class ExternalLoginResult : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    var result = OAuthWeb.VerifyAuthorization();
    Response.Write(String.Format("Provider: {0}<br />", result.ProviderName));
    if (result.IsSuccessfully)
    {
      var user = result.UserInfo;
      Response.Write(String.Format("User ID:  {0}<br />", user.UserId));
      Response.Write(String.Format("Name:     {0}<br />", user.DisplayName));
      Response.Write(String.Format("Email:    {0}", user.Email));
    }
    else
    {
      Response.Write(result.ErrorInfo.Message);
    }
  }
}

Visual Basic .NET

Public Class ExternalLoginResult
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim result As AuthorizationResult = OAuthWeb.VerifyAuthorization()
    Response.Write(String.Format("Provider: {0}<br />", result.ProviderName))
    If result.IsSuccessfully Then
      Dim user As UserInfo = result.UserInfo
      Response.Write(String.Format("User ID:  {0}<br />", user.UserId))
      Response.Write(String.Format("Name:     {0}<br />", user.DisplayName))
      Response.Write(String.Format("Email:    {0}", user.Email))
    Else
      Response.Write(result.ErrorInfo.ToString())
    End If
  End Sub

End Class

4. Get the address for authentication and redirect the user to it.

C#

string returnUrl =  new Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri;
OAuthWeb.RedirectToAuthorization("facebook", returnUrl);

Visual Basic .NET

Dim returnUrl As String = New Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri
OAuthWeb.RedirectToAuthorization("facebook", returnUrl)

5. Enjoy!

See Also