Represents helper class for sessions management of OAuth.
System Object Nemiro.OAuth OAuthWebNamespace: Nemiro.OAuthAssembly: Nemiro.OAuth (in Nemiro.OAuth.dll) Version: 1.9.4.725 (1.9.4.725)
public static class OAuthWeb
public static class OAuthWeb
Public NotInheritable Class OAuthWeb
Public NotInheritable Class OAuthWeb
| Name | Description |
---|
  | GetAuthorizationUrl(String) |
Returns the authorization URL of the specified provider.
|
  | GetAuthorizationUrl(String, NameValueCollection) |
Returns the authorization URL of the specified provider with specified parameters.
|
  | GetAuthorizationUrl(String, String) |
Returns the authorization URL of the specified provider and return URL.
|
  | GetAuthorizationUrl(String, NameValueCollection, String) |
Returns the authorization URL of the specified provider, query parameters and return URL.
|
  | GetAuthorizationUrl(ClientName, NameValueCollection, String) |
Returns the authorization URL of the specified provider, query parameters and return URL.
|
  | RedirectToAuthorization(String) |
Redirects current client to the authorization page of the specified provider.
|
  | RedirectToAuthorization(String, NameValueCollection) |
Redirects current client to the authorization page of the specified provider with specified parameters.
|
  | RedirectToAuthorization(String, String) |
Redirects current client to the authorization page of the specified provider and return URL.
|
  | RedirectToAuthorization(String, NameValueCollection, String) |
Redirects current client to the authorization page of the specified provider, query parameters and return URL.
|
  | RedirectToAuthorization(ClientName, NameValueCollection, String) |
Redirects current client to the authorization page of the specified provider, query parameters and return URL.
|
  | VerifyAuthorization |
Verifies the authorization results for the current URL.
|
  | VerifyAuthorization(String) |
Verifies the authorization results for the specified URL.
|
  | VerifyAuthorization(String, String) |
Verifies the authorization results for the specified request identifier and the code of the authorization.
|
  | VerifyAuthorizationAndRemoveRequest |
Verifies the authorization results for the current URL and removes the request from memory.
|
  | VerifyAuthorizationAndRemoveRequest(String) |
Verifies the authorization results for the specified URL, and removes the request from memory.
|
  | VerifyAuthorizationAndRemoveRequest(String, String) |
Verifies the authorization results for the specified request identifier and the code of the authorization, and removes the request from memory.
|
TopMainly the class is intended for web projects.
But you can use some methods of the class in desktop applications together with the WebBrowser control.
The methods RedirectToAuthorization(String),
RedirectToAuthorization(String, String),
RedirectToAuthorization(String, NameValueCollection, String) and
VerifyAuthorization
will not work in desktop applications.
ASP .NET WebForms
The following example shows how to use the TwitterClient in ASP .NET WebForms.
To redirect the user to the login page is used the RedirectToAuthorization(String, String) method.
Processing of the authorization results is performed by VerifyAuthorization .
To test the example, create a new ASP .NET WebForms (empty) project. Add Global.asax.
In the Application_Start event handler (Global.asax file) is registered the TwitterClient.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Nemiro.OAuth;
using Nemiro.OAuth.Clients;
namespace Test.CSharp.AspWebForms
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
OAuthManager.RegisterClient
(
new TwitterClient
(
"cXzSHLUy57C4gTBgMGRDuqQtr",
"3SSldiSb5H4XeEMOIIF4osPWxOy19jrveDcPHaWtHDQqgDYP9P"
)
);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Nemiro.OAuth;
using Nemiro.OAuth.Clients;
namespace Test.CSharp.AspWebForms
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
OAuthManager.RegisterClient
(
new TwitterClient
(
"cXzSHLUy57C4gTBgMGRDuqQtr",
"3SSldiSb5H4XeEMOIIF4osPWxOy19jrveDcPHaWtHDQqgDYP9P"
)
);
}
}
}
Imports Nemiro.OAuth
Imports Nemiro.OAuth.Clients
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
OAuthManager.RegisterClient _
(
New TwitterClient _
(
"cXzSHLUy57C4gTBgMGRDuqQtr",
"3SSldiSb5H4XeEMOIIF4osPWxOy19jrveDcPHaWtHDQqgDYP9P"
)
)
End Sub
End Class
Imports Nemiro.OAuth
Imports Nemiro.OAuth.Clients
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
OAuthManager.RegisterClient _
(
New TwitterClient _
(
"cXzSHLUy57C4gTBgMGRDuqQtr",
"3SSldiSb5H4XeEMOIIF4osPWxOy19jrveDcPHaWtHDQqgDYP9P"
)
)
End Sub
End Class
Add ExternalLoginResult.aspx.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExternalLoginResult.aspx.cs" Inherits="Test.CSharp.AspWebForms.ExternalLoginResult" %gt;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExternalLoginResult.aspx.cs" Inherits="Test.CSharp.AspWebForms.ExternalLoginResult" %gt;
And add the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;
namespace Test.CSharp.AspWebForms
{
public partial class ExternalLoginResult : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<pre>");
var result = OAuthWeb.VerifyAuthorization();
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));
}
else
{
Response.Write(result.ErrorInfo.Message);
}
Response.Write("</pre>");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;
namespace Test.CSharp.AspWebForms
{
public partial class ExternalLoginResult : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<pre>");
var result = OAuthWeb.VerifyAuthorization();
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));
}
else
{
Response.Write(result.ErrorInfo.Message);
}
Response.Write("</pre>");
}
}
}
Imports Nemiro.OAuth
Public Class ExternalLoginResult
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write("<pre>")
Dim result As AuthorizationResult = OAuthWeb.VerifyAuthorization()
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))
Else
Response.Write(result.ErrorInfo.Message)
End If
Response.Write("</pre>")
End Sub
End Class
Imports Nemiro.OAuth
Public Class ExternalLoginResult
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write("<pre>")
Dim result As AuthorizationResult = OAuthWeb.VerifyAuthorization()
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))
Else
Response.Write(result.ErrorInfo.Message)
End If
Response.Write("</pre>")
End Sub
End Class
Add Default.aspx and insert one LinkButton to the page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.CSharp.AspWebForms.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="lnkTwitter" runat="server"
Text="Log in with Twitter" onclick="lnkTwitter_Click" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.CSharp.AspWebForms.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="lnkTwitter" runat="server"
Text="Log in with Twitter" onclick="lnkTwitter_Click" />
</div>
</form>
</body>
</html>
Add a handler for a click on the link.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;
namespace Test.CSharp.AspWebForms
{
public partial class Default : System.Web.UI.Page
{
protected void lnkTwitter_Click(object sender, EventArgs e)
{
OAuthWeb.RedirectToAuthorization("Twitter", new Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nemiro.OAuth;
namespace Test.CSharp.AspWebForms
{
public partial class Default : System.Web.UI.Page
{
protected void lnkTwitter_Click(object sender, EventArgs e)
{
OAuthWeb.RedirectToAuthorization("Twitter", new Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri);
}
}
}
Imports Nemiro.OAuth
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub lnkTwitter_Click(sender As Object, e As EventArgs) Handles lnkTwitter.Click
OAuthWeb.RedirectToAuthorization("Twitter", New Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri)
End Sub
End Class
Imports Nemiro.OAuth
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub lnkTwitter_Click(sender As Object, e As EventArgs) Handles lnkTwitter.Click
OAuthWeb.RedirectToAuthorization("Twitter", New Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri)
End Sub
End Class
Windows Forms
The following example shows how to use the MailRuClient in desktop applications.
Methods redirection in Windows Forms applications do not work. To get the address of the authorization GetAuthorizationUrl(String) is used.
To test the example, create a new Windows Forms project with two forms. Add a Button to the Form1.
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
OAuthManager.RegisterClient
(
new MailRuClient
(
"722701",
"d0622d3d9c9efc69e4ca42aa173b938a"
)
);
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.ShowDialog();
}
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
}
private void Form1_Load(object sender, EventArgs e)
{
// mail.ru client registration
OAuthManager.RegisterClient
(
new MailRuClient
(
"722701",
"d0622d3d9c9efc69e4ca42aa173b938a"
)
);
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.ShowDialog();
}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
OAuthManager.RegisterClient _
(
New MailRuClient _
(
"722701",
"d0622d3d9c9efc69e4ca42aa173b938a"
)
)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Call New Form2().ShowDialog()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' mail.ru client registration
OAuthManager.RegisterClient _
(
New MailRuClient _
(
"722701",
"d0622d3d9c9efc69e4ca42aa173b938a"
)
)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Call New Form2().ShowDialog()
End Sub
Add a WebBrowser to the Form2.
public Form2()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate(OAuthWeb.GetAuthorizationUrl("Mail.Ru"));
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.Query.IndexOf("code=") != -1 || e.Url.Fragment.IndexOf("code=") != -1 || e.Url.Query.IndexOf("oauth_verifier=") != -1)
{
var result = OAuthWeb.VerifyAuthorization(e.Url.ToString());
if (result.IsSuccessfully)
{
MessageBox.Show
(
String.Format
(
"User ID: {0}\r\nUsername: {1}\r\nDisplay Name: {2}\r\nE-Mail: {3}",
result.UserInfo.UserId,
result.UserInfo.UserName,
result.UserInfo.DisplayName ?? result.UserInfo.FullName,
result.UserInfo.Email
),
"Successfully",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
else
{
MessageBox.Show(result.ErrorInfo.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
}
}
public Form2()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate(OAuthWeb.GetAuthorizationUrl("Mail.Ru"));
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// waiting for results
if (e.Url.Query.IndexOf("code=") != -1 || e.Url.Fragment.IndexOf("code=") != -1 || e.Url.Query.IndexOf("oauth_verifier=") != -1)
{
// is the result, verify
var result = OAuthWeb.VerifyAuthorization(e.Url.ToString());
if (result.IsSuccessfully)
{
// show user info
MessageBox.Show
(
String.Format
(
"User ID: {0}\r\nUsername: {1}\r\nDisplay Name: {2}\r\nE-Mail: {3}",
result.UserInfo.UserId,
result.UserInfo.UserName,
result.UserInfo.DisplayName ?? result.UserInfo.FullName,
result.UserInfo.Email
),
"Successfully",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
else
{
// show error message
MessageBox.Show(result.ErrorInfo.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
}
}
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(OAuthWeb.GetAuthorizationUrl("Mail.Ru"))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If Not e.Url.Query.IndexOf("code=") = -1 OrElse Not e.Url.Fragment.IndexOf("code=") = -1 OrElse Not e.Url.Query.IndexOf("oauth_verifier=") = -1 Then
Dim result = OAuthWeb.VerifyAuthorization(e.Url.ToString())
If result.IsSuccessfully Then
MessageBox.Show _
(
String.Format _
(
"User ID: {0}{4}Username: {1}{4}Display Name: {2}{4}E-Mail: {3}",
result.UserInfo.UserId,
result.UserInfo.UserName,
If(Not String.IsNullOrEmpty(result.UserInfo.DisplayName), result.UserInfo.DisplayName, result.UserInfo.FullName),
result.UserInfo.Email,
vbNewLine
),
"Successfully",
MessageBoxButtons.OK,
MessageBoxIcon.Information
)
Else
MessageBox.Show(result.ErrorInfo.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Me.Close()
End If
End Sub
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(OAuthWeb.GetAuthorizationUrl("Mail.Ru"))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' waiting for results
If Not e.Url.Query.IndexOf("code=") = -1 OrElse Not e.Url.Fragment.IndexOf("code=") = -1 OrElse Not e.Url.Query.IndexOf("oauth_verifier=") = -1 Then
' is the result, verify
Dim result = OAuthWeb.VerifyAuthorization(e.Url.ToString())
If result.IsSuccessfully Then
' show user info
MessageBox.Show _
(
String.Format _
(
"User ID: {0}{4}Username: {1}{4}Display Name: {2}{4}E-Mail: {3}",
result.UserInfo.UserId,
result.UserInfo.UserName,
If(Not String.IsNullOrEmpty(result.UserInfo.DisplayName), result.UserInfo.DisplayName, result.UserInfo.FullName),
result.UserInfo.Email,
vbNewLine
),
"Successfully",
MessageBoxButtons.OK,
MessageBoxIcon.Information
)
Else
' show error message
MessageBox.Show(result.ErrorInfo.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Me.Close()
End If
End Sub