diff options
author | András Fuchs <andras.fuchs@gmail.com> | 2013-05-26 07:54:53 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-05-26 07:54:53 -0700 |
commit | 2a4da5e544a22d30d5b54a2696194d814d9a442f (patch) | |
tree | ed76c7421e9669bbf263381139a29afb8eb23433 /samples/OAuthClient | |
parent | 85c21ae12f04cc50a0478cf69c82d441da23d002 (diff) | |
download | DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.zip DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.tar.gz DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.tar.bz2 |
Samples improvements
The part which I needed
to improve is the ApplicationBlock where I changed the OAuth2 classes'
structure a little and extended them with a lot of useful
functionality, like adding many Facebook and WindowsLive scopes,
fields, structures including the asked-by-many easy to use birthdate
and avatar url getters. I have also implemented the Google OAuth2
authentication and created one common interface for all 3 Graphs in
the code (which has the common properties like Id, FirstName,
LastName, etc.), so the authentication code became really simple if
you use my version of your ApplicationBlock.
Diffstat (limited to 'samples/OAuthClient')
-rw-r--r-- | samples/OAuthClient/Default.aspx | 5 | ||||
-rw-r--r-- | samples/OAuthClient/Facebook.aspx.cs | 38 | ||||
-rw-r--r-- | samples/OAuthClient/Google.aspx | 16 | ||||
-rw-r--r-- | samples/OAuthClient/Google.aspx.cs | 37 | ||||
-rw-r--r-- | samples/OAuthClient/Google.aspx.designer.cs | 33 | ||||
-rw-r--r-- | samples/OAuthClient/OAuthClient.csproj | 15 | ||||
-rw-r--r-- | samples/OAuthClient/Web.config | 3 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx.cs | 45 | ||||
-rw-r--r-- | samples/OAuthClient/packages.config | 4 |
9 files changed, 157 insertions, 39 deletions
diff --git a/samples/OAuthClient/Default.aspx b/samples/OAuthClient/Default.aspx index 79d0acf..d04066d 100644 --- a/samples/OAuthClient/Default.aspx +++ b/samples/OAuthClient/Default.aspx @@ -9,9 +9,10 @@ <li><a href="GoogleAddressBook.aspx">Download your Gmail address book</a></li> <li><a href="Twitter.aspx">Get your Twitter updates</a></li> <li><a href="SignInWithTwitter.aspx">Sign In With Twitter</a></li> - <li><a href="Facebook.aspx">Sign in with Facebook</a></li> - <li><a href="WindowsLive.aspx">Sign in with Windows Live</a></li> <li><a href="AzureAD.aspx">Sign in with Azure Active Directory\Office 365</a></li> + <li><a href="Facebook.aspx">Sign in with Facebook (OAuth 2.0)</a></li> + <li><a href="WindowsLive.aspx">Sign in with Windows Live (OAuth 2.0)</a></li> + <li><a href="Google.aspx">Sign in with Google (OAuth 2.0) [check your web.config and set the googleClientID and googleClientSecret values before testing]</a></li> <li><a href="SampleWcf2.aspx">Interop with Authorization Server sample (Authorization code grant) and Resource Server using WCF w/ OAuth 2.0 </a></li> <li><a href="SampleWcf2Javascript.html">Interop with Authorization Server sample (implicit grant) and Resource Server using WCF w/ OAuth 2.0 </a></li> </ul> diff --git a/samples/OAuthClient/Facebook.aspx.cs b/samples/OAuthClient/Facebook.aspx.cs index 4701d24..e7261a0 100644 --- a/samples/OAuthClient/Facebook.aspx.cs +++ b/samples/OAuthClient/Facebook.aspx.cs @@ -1,31 +1,37 @@ -namespace OAuthClient { +namespace OAuthClient +{ using System; using System.Configuration; using System.Net; using System.Web; using DotNetOpenAuth.ApplicationBlock; - using DotNetOpenAuth.ApplicationBlock.Facebook; using DotNetOpenAuth.OAuth2; - public partial class Facebook : System.Web.UI.Page { - private static readonly FacebookClient client = new FacebookClient { + public partial class Facebook : System.Web.UI.Page + { + private static readonly FacebookClient facebookClient = new FacebookClient + { ClientIdentifier = ConfigurationManager.AppSettings["facebookAppID"], ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["facebookAppSecret"]), }; - protected void Page_Load(object sender, EventArgs e) { - IAuthorizationState authorization = client.ProcessUserAuthorization(); - if (authorization == null) { + protected void Page_Load(object sender, EventArgs e) + { + IAuthorizationState authorization = facebookClient.ProcessUserAuthorization(); + if (authorization == null) + { // Kick off authorization request - client.RequestUserAuthorization(); - } else { - var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); - using (var response = request.GetResponse()) { - using (var responseStream = response.GetResponseStream()) { - var graph = FacebookGraph.Deserialize(responseStream); - this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); - } - } + facebookClient.RequestUserAuthorization(); + + // alternatively you can ask for more information + // facebookClient.RequestUserAuthorization(scope: new[] { FacebookClient.Scopes.Email, FacebookClient.Scopes.UserBirthday }); + } + else + { + IOAuth2Graph oauth2Graph = facebookClient.GetGraph(authorization); + // IOAuth2Graph oauth2Graph = facebookClient.GetGraph(authorization, new[] { FacebookGraph.Fields.Defaults, FacebookGraph.Fields.Email, FacebookGraph.Fields.Picture, FacebookGraph.Fields.Birthday }); + + this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name); } } } diff --git a/samples/OAuthClient/Google.aspx b/samples/OAuthClient/Google.aspx new file mode 100644 index 0000000..a9eae90 --- /dev/null +++ b/samples/OAuthClient/Google.aspx @@ -0,0 +1,16 @@ +<%@ Page Language="C#" AutoEventWireup="true" Inherits="OAuthClient.Google" Codebehind="Google.aspx.cs" %> + +<!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> + Welcome, + <asp:Label Text="[name]" ID="nameLabel" runat="server" /> + </div> + </form> +</body> +</html> diff --git a/samples/OAuthClient/Google.aspx.cs b/samples/OAuthClient/Google.aspx.cs new file mode 100644 index 0000000..fda643e --- /dev/null +++ b/samples/OAuthClient/Google.aspx.cs @@ -0,0 +1,37 @@ +namespace OAuthClient +{ + using System; + using System.Configuration; + using System.Net; + using System.Web; + using DotNetOpenAuth.ApplicationBlock; + using DotNetOpenAuth.OAuth2; + + public partial class Google : System.Web.UI.Page + { + private static readonly GoogleClient googleClient = new GoogleClient + { + ClientIdentifier = ConfigurationManager.AppSettings["googleClientID"], + ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["googleClientSecret"]), + }; + + protected void Page_Load(object sender, EventArgs e) + { + IAuthorizationState authorization = googleClient.ProcessUserAuthorization(); + if (authorization == null) + { + // Kick off authorization request + googleClient.RequestUserAuthorization(); + + // alternatively you can ask for more information + // googleClient.RequestUserAuthorization(scope: new[] { GoogleClient.Scopes.UserInfo.Profile, GoogleClient.Scopes.UserInfo.Email }); + } + else + { + IOAuth2Graph oauth2Graph = googleClient.GetGraph(authorization); + + this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name); + } + } + } +}
\ No newline at end of file diff --git a/samples/OAuthClient/Google.aspx.designer.cs b/samples/OAuthClient/Google.aspx.designer.cs new file mode 100644 index 0000000..4c7ecad --- /dev/null +++ b/samples/OAuthClient/Google.aspx.designer.cs @@ -0,0 +1,33 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace OAuthClient { + + + public partial class Google { + + /// <summary> + /// form1 control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.HtmlControls.HtmlForm form1; + + /// <summary> + /// nameLabel control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Label nameLabel; + } +} diff --git a/samples/OAuthClient/OAuthClient.csproj b/samples/OAuthClient/OAuthClient.csproj index 9fbcc5d..3172562 100644 --- a/samples/OAuthClient/OAuthClient.csproj +++ b/samples/OAuthClient/OAuthClient.csproj @@ -10,6 +10,7 @@ <IISExpressAnonymousAuthentication /> <IISExpressWindowsAuthentication /> <IISExpressUseClassicPipelineMode /> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\src\</SolutionDir> </PropertyGroup> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> @@ -23,7 +24,7 @@ <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>OAuthClient</RootNamespace> <AssemblyName>OAuthClient</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkProfile /> <UseIISExpress>true</UseIISExpress> </PropertyGroup> @@ -49,7 +50,7 @@ </PropertyGroup> <ItemGroup> <Reference Include="log4net"> - <HintPath>..\..\lib\log4net.dll</HintPath> + <HintPath>..\..\src\packages\log4net.2.0.0\lib\net40-full\log4net.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> @@ -71,6 +72,7 @@ </ItemGroup> <ItemGroup> <Content Include="AzureAD.aspx" /> + <Content Include="Google.aspx" /> <Content Include="Default.aspx" /> <Content Include="Facebook.aspx" /> <Content Include="favicon.ico" /> @@ -84,6 +86,7 @@ <Content Include="Scripts\jquery-1.6.1.min.js" /> <Content Include="WindowsLive.aspx" /> <Content Include="Yammer.aspx" /> + <Content Include="packages.config" /> <None Include="Service References\SampleResourceServer\DataApi.disco" /> <None Include="Service References\SampleResourceServer\configuration91.svcinfo" /> <None Include="Service References\SampleResourceServer\configuration.svcinfo" /> @@ -114,6 +117,13 @@ <Compile Include="AzureAD.aspx.designer.cs"> <DependentUpon>AzureAD.aspx</DependentUpon> </Compile> + <Compile Include="Google.aspx.cs"> + <DependentUpon>Google.aspx</DependentUpon> + <SubType>ASPXCodeBehind</SubType> + </Compile> + <Compile Include="Google.aspx.designer.cs"> + <DependentUpon>Google.aspx</DependentUpon> + </Compile> <Compile Include="Facebook.aspx.cs"> <DependentUpon>Facebook.aspx</DependentUpon> <SubType>ASPXCodeBehind</SubType> @@ -262,4 +272,5 @@ </VisualStudio> </ProjectExtensions> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), EnlistmentInfo.targets))\EnlistmentInfo.targets" Condition=" '$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), EnlistmentInfo.targets))' != '' " /> + <Import Project="$(SolutionDir)\.nuget\nuget.targets" /> </Project>
\ No newline at end of file diff --git a/samples/OAuthClient/Web.config b/samples/OAuthClient/Web.config index 9463ced..35984fc 100644 --- a/samples/OAuthClient/Web.config +++ b/samples/OAuthClient/Web.config @@ -46,6 +46,9 @@ <!-- Google sign-up: https://www.google.com/accounts/ManageDomains --> <add key="googleConsumerKey" value="anonymous"/> <add key="googleConsumerSecret" value="anonymous"/> + <!--Google API sign-up: https://code.google.com/apis/console/ (OAuth2) --> + <add key="googleClientID" value="" /> + <add key="googleClientSecret" value="" /> <!-- Yammer sign-up: https://www.yammer.com/client_applications/new --> <add key="yammerConsumerKey" value=""/> <add key="yammerConsumerSecret" value=""/> diff --git a/samples/OAuthClient/WindowsLive.aspx.cs b/samples/OAuthClient/WindowsLive.aspx.cs index 05101a7..b28bcc3 100644 --- a/samples/OAuthClient/WindowsLive.aspx.cs +++ b/samples/OAuthClient/WindowsLive.aspx.cs @@ -1,4 +1,5 @@ -namespace OAuthClient { +namespace OAuthClient +{ using System; using System.Collections.Generic; using System.Configuration; @@ -8,36 +9,42 @@ using System.Web.UI; using System.Web.UI.WebControls; using DotNetOpenAuth.ApplicationBlock; - using DotNetOpenAuth.ApplicationBlock.Facebook; using DotNetOpenAuth.OAuth2; - public partial class WindowsLive : System.Web.UI.Page { - private static readonly WindowsLiveClient client = new WindowsLiveClient { + public partial class WindowsLive : System.Web.UI.Page + { + private static readonly WindowsLiveClient windowsLiveClient = new WindowsLiveClient + { ClientIdentifier = ConfigurationManager.AppSettings["windowsLiveAppID"], ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["WindowsLiveAppSecret"]), }; - protected void Page_Load(object sender, EventArgs e) { - if (string.Equals("localhost", this.Request.Headers["Host"].Split(':')[0], StringComparison.OrdinalIgnoreCase)) { + protected void Page_Load(object sender, EventArgs e) + { + if (string.Equals("localhost", this.Request.Headers["Host"].Split(':')[0], StringComparison.OrdinalIgnoreCase)) + { this.localhostDoesNotWorkPanel.Visible = true; var builder = new UriBuilder(this.publicLink.NavigateUrl); builder.Port = this.Request.Url.Port; this.publicLink.NavigateUrl = builder.Uri.AbsoluteUri; this.publicLink.Text = builder.Uri.AbsoluteUri; - } else { - IAuthorizationState authorization = client.ProcessUserAuthorization(); - if (authorization == null) { + } + else + { + IAuthorizationState authorization = windowsLiveClient.ProcessUserAuthorization(); + if (authorization == null) + { // Kick off authorization request - client.RequestUserAuthorization(scope: new[] { WindowsLiveClient.Scopes.Basic }); // this scope isn't even required just to log in - } else { - var request = - WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); - using (var response = request.GetResponse()) { - using (var responseStream = response.GetResponseStream()) { - var graph = WindowsLiveGraph.Deserialize(responseStream); - this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); - } - } + windowsLiveClient.RequestUserAuthorization(scope: new[] { WindowsLiveClient.Scopes.Basic }); // this scope isn't even required just to log in + + // alternatively you can ask for more information + // windowsLiveClient.RequestUserAuthorization(scope: new[] { WindowsLiveClient.Scopes.SignIn, WindowsLiveClient.Scopes.Emails, WindowsLiveClient.Scopes.Birthday }); + } + else + { + IOAuth2Graph oauth2Graph = windowsLiveClient.GetGraph(authorization); + + this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name); } } } diff --git a/samples/OAuthClient/packages.config b/samples/OAuthClient/packages.config new file mode 100644 index 0000000..6562527 --- /dev/null +++ b/samples/OAuthClient/packages.config @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="log4net" version="2.0.0" targetFramework="net45" /> +</packages>
\ No newline at end of file |