diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-07 19:26:03 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-07 19:26:03 -0800 |
commit | 4377c981006a129ca659cbf639aa0959a3b267cf (patch) | |
tree | ed013488b0f18cfb304c33762112544681299c4b | |
parent | dddacc0f95cc324e4e00c58537b544af2f92719c (diff) | |
download | DotNetOpenAuth-4377c981006a129ca659cbf639aa0959a3b267cf.zip DotNetOpenAuth-4377c981006a129ca659cbf639aa0959a3b267cf.tar.gz DotNetOpenAuth-4377c981006a129ca659cbf639aa0959a3b267cf.tar.bz2 |
Adds a Windows Live login sample.
Fixes #83
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj | 2 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveClient.cs | 53 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveGraph.cs | 60 | ||||
-rw-r--r-- | samples/OAuthClient/Default.aspx | 1 | ||||
-rw-r--r-- | samples/OAuthClient/OAuthClient.csproj | 8 | ||||
-rw-r--r-- | samples/OAuthClient/Web.config | 3 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx | 29 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx.cs | 46 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx.designer.cs | 51 |
9 files changed, 253 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj index 43b4a00..522a8ab 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -101,6 +101,8 @@ <Compile Include="TokenManager.cs" /> <Compile Include="TwitterConsumer.cs" /> <Compile Include="Util.cs" /> + <Compile Include="WindowsLiveClient.cs" /> + <Compile Include="WindowsLiveGraph.cs" /> <Compile Include="YammerConsumer.cs" /> <Compile Include="YubikeyRelyingParty.cs" /> </ItemGroup> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveClient.cs new file mode 100644 index 0000000..5b8582d --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveClient.cs @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------- +// <copyright file="WindowsLiveClient.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using DotNetOpenAuth.OAuth2; + + public class WindowsLiveClient : WebServerClient { + private static readonly AuthorizationServerDescription WindowsLiveDescription = new AuthorizationServerDescription { + TokenEndpoint = new Uri("https://oauth.live.com/token"), + AuthorizationEndpoint = new Uri("https://oauth.live.com/authorize"), + }; + + /// <summary> + /// Initializes a new instance of the <see cref="WindowsLiveClient"/> class. + /// </summary> + public WindowsLiveClient() + : base(WindowsLiveDescription) { + this.AuthorizationTracker = new TokenManager(); + } + + /// <summary> + /// Well-known scopes defined by the Windows Live service. + /// </summary> + /// <remarks> + /// This sample includes just a few scopes. For a complete list of scopes please refer to: + /// http://msdn.microsoft.com/en-us/library/hh243646.aspx + /// </remarks> + public static class Scopes { + /// <summary> + /// The ability of an app to read and update a user's info at any time. Without this scope, an app can access the user's info only while the user is signed in to Live Connect and is using your app. + /// </summary> + public const string OfflineAccess = "wl.offline_access"; + + /// <summary> + /// Single sign-in behavior. With single sign-in, users who are already signed in to Live Connect are also signed in to your website. + /// </summary> + public const string SignIn = "wl.signin"; + + /// <summary> + /// Read access to a user's basic profile info. Also enables read access to a user's list of contacts. + /// </summary> + public const string Basic = "wl.basic"; + } + } + +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveGraph.cs b/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveGraph.cs new file mode 100644 index 0000000..4801226 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/WindowsLiveGraph.cs @@ -0,0 +1,60 @@ +//----------------------------------------------------------------------- +// <copyright file="WindowsLiveGraph.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Runtime.Serialization; + using System.Runtime.Serialization.Json; + using System.Text; + + [DataContract] + public class WindowsLiveGraph { + private static DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(WindowsLiveGraph)); + + [DataMember(Name = "id")] + public string Id { get; set; } + + [DataMember(Name = "name")] + public string Name { get; set; } + + [DataMember(Name = "first_name")] + public string FirstName { get; set; } + + [DataMember(Name = "last_name")] + public string LastName { get; set; } + + [DataMember(Name = "link")] + public Uri Link { get; set; } + + [DataMember(Name = "gender")] + public string Gender { get; set; } + + [DataMember(Name = "updated_time")] + public string UpdatedTime { get; set; } + + [DataMember(Name = "locale")] + public string Locale { get; set; } + + public static WindowsLiveGraph Deserialize(string json) { + if (string.IsNullOrEmpty(json)) { + throw new ArgumentNullException("json"); + } + + return Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(json))); + } + + public static WindowsLiveGraph Deserialize(Stream jsonStream) { + if (jsonStream == null) { + throw new ArgumentNullException("jsonStream"); + } + + return (WindowsLiveGraph)jsonSerializer.ReadObject(jsonStream); + } + } +} diff --git a/samples/OAuthClient/Default.aspx b/samples/OAuthClient/Default.aspx index 0452033..f832ccf 100644 --- a/samples/OAuthClient/Default.aspx +++ b/samples/OAuthClient/Default.aspx @@ -10,6 +10,7 @@ <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="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/OAuthClient.csproj b/samples/OAuthClient/OAuthClient.csproj index 9aeb0d1..4043e6d 100644 --- a/samples/OAuthClient/OAuthClient.csproj +++ b/samples/OAuthClient/OAuthClient.csproj @@ -70,6 +70,7 @@ <Content Include="SampleWcf2Javascript.js" /> <Content Include="Scripts\jquery-1.6.1.js" /> <Content Include="Scripts\jquery-1.6.1.min.js" /> + <Content Include="WindowsLive.aspx" /> <Content Include="Yammer.aspx" /> <None Include="Service References\SampleResourceServer\DataApi.disco" /> <None Include="Service References\SampleResourceServer\configuration91.svcinfo" /> @@ -154,6 +155,13 @@ <Compile Include="Twitter.aspx.designer.cs"> <DependentUpon>Twitter.aspx</DependentUpon> </Compile> + <Compile Include="WindowsLive.aspx.cs"> + <DependentUpon>WindowsLive.aspx</DependentUpon> + <SubType>ASPXCodeBehind</SubType> + </Compile> + <Compile Include="WindowsLive.aspx.designer.cs"> + <DependentUpon>WindowsLive.aspx</DependentUpon> + </Compile> <Compile Include="Yammer.aspx.cs"> <DependentUpon>Yammer.aspx</DependentUpon> <SubType>ASPXCodeBehind</SubType> diff --git a/samples/OAuthClient/Web.config b/samples/OAuthClient/Web.config index 9fa30d9..1a54474 100644 --- a/samples/OAuthClient/Web.config +++ b/samples/OAuthClient/Web.config @@ -64,6 +64,9 @@ <!-- Facebook sign-up: http://developers.facebook.com/setup/ --> <add key="facebookAppID" value="367207604173"/> <add key="facebookAppSecret" value="1df77e64055c4d7d3583cefdf2bc62d7"/> + <!-- Windows Live sign-up: http://go.microsoft.com/fwlink/p/?LinkId=193157 --> + <add key="windowsLiveAppID" value="000000004408E558" /> + <add key="windowsLiveAppSecret" value="od8NVdanEIWqmlKu9hOepBE3AfUu4jCw" /> </appSettings> <connectionStrings/> diff --git a/samples/OAuthClient/WindowsLive.aspx b/samples/OAuthClient/WindowsLive.aspx new file mode 100644 index 0000000..5b8c8d4 --- /dev/null +++ b/samples/OAuthClient/WindowsLive.aspx @@ -0,0 +1,29 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WindowsLive.aspx.cs" Inherits="OAuthClient.WindowsLive" %> + +<!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"> + <asp:Panel runat="server" ID="localhostDoesNotWorkPanel" Visible="False"> + <p> + Windows Live requires a public domain (not localhost) that matches the registered + client app's callback URL. You can either host this sample on a public URL and register + your own client, or you can modify your "%windows%\system32\drivers\etc\hosts" file + to temporarily add this entry: + </p> + <pre>127.0.0.1 samples.dotnetopenauth.net</pre> + <p> + Then access this sample via this url: + <asp:HyperLink ID="publicLink" NavigateUrl="http://samples.dotnetopenauth.net:59721/WindowsLive.aspx" + runat="server">http://samples.dotnetopenauth.net:59721/WindowsLive.aspx</asp:HyperLink></p> + </asp:Panel> + <div> + Welcome, + <asp:Label Text="[name]" ID="nameLabel" runat="server" /> + </div> + </form> +</body> +</html> diff --git a/samples/OAuthClient/WindowsLive.aspx.cs b/samples/OAuthClient/WindowsLive.aspx.cs new file mode 100644 index 0000000..ab5793b --- /dev/null +++ b/samples/OAuthClient/WindowsLive.aspx.cs @@ -0,0 +1,46 @@ +namespace OAuthClient { + using System; + using System.Collections.Generic; + using System.Configuration; + using System.Linq; + using System.Net; + using System.Web; + 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 { + ClientIdentifier = ConfigurationManager.AppSettings["windowsLiveAppID"], + ClientSecret = ConfigurationManager.AppSettings["WindowsLiveAppSecret"], + }; + + protected void Page_Load(object sender, EventArgs e) { + if (String.Equals("localhost", this.Request.Headers["Host"].Split(':')[0], StringComparison.OrdinalIgnoreCase)) { + localhostDoesNotWorkPanel.Visible = true; + var builder = new UriBuilder(publicLink.NavigateUrl); + builder.Port = this.Request.Url.Port; + publicLink.NavigateUrl = builder.Uri.AbsoluteUri; + publicLink.Text = builder.Uri.AbsoluteUri; + } else { + IAuthorizationState authorization = client.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); + } + } + } + } + } + } +} diff --git a/samples/OAuthClient/WindowsLive.aspx.designer.cs b/samples/OAuthClient/WindowsLive.aspx.designer.cs new file mode 100644 index 0000000..aa47128 --- /dev/null +++ b/samples/OAuthClient/WindowsLive.aspx.designer.cs @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// <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 WindowsLive { + + /// <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> + /// localhostDoesNotWorkPanel 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.Panel localhostDoesNotWorkPanel; + + /// <summary> + /// publicLink 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.HyperLink publicLink; + + /// <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; + } +} |