Created
November 6, 2012 13:10
-
-
Save gasparnagy/4024635 to your computer and use it in GitHub Desktop.
DotNetOpenAuth client for XING
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Xml; | |
using System.Xml.Linq; | |
using DotNetOpenAuth.AspNet; | |
using DotNetOpenAuth.AspNet.Clients; | |
using DotNetOpenAuth.Messaging; | |
using DotNetOpenAuth.OAuth; | |
using DotNetOpenAuth.OAuth.ChannelElements; | |
using DotNetOpenAuth.OAuth.Messages; | |
namespace TechTalk.ExpertsPortal.Web.Infrastructure | |
{ | |
public class XingClient : OAuthClient | |
{ | |
public static readonly ServiceProviderDescription TwitterServiceDescription = new ServiceProviderDescription | |
{ | |
RequestTokenEndpoint = | |
new MessageReceivingEndpoint( | |
"https://api.xing.com/v1/request_token", | |
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), | |
UserAuthorizationEndpoint = | |
new MessageReceivingEndpoint( | |
"https://api.xing.com/v1/authorize", | |
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), | |
AccessTokenEndpoint = | |
new MessageReceivingEndpoint( | |
"https://api.xing.com/v1/access_token", | |
HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), | |
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new PlaintextSigningBindingElement()} | |
}; | |
public XingClient(string consumerKey, string consumerSecret) : | |
base("XING", TwitterServiceDescription, consumerKey, consumerSecret) | |
{ | |
} | |
public XingClient(string consumerKey, string consumerSecret, IOAuthTokenManager tokenManager) : | |
base("XING", TwitterServiceDescription, new SimpleConsumerTokenManager(consumerKey, consumerSecret, tokenManager)) | |
{ | |
} | |
protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) | |
{ | |
string accessToken = response.AccessToken; | |
string userId = response.ExtraData["user_id"]; | |
string userName = userId; | |
var profileRequestUrl = new Uri("https://api.xing.com/v1/users/me.xml?fields=permalink,active_email,display_name"); | |
var profileEndpoint = new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest); | |
HttpWebRequest request = this.WebWorker.PrepareAuthorizedRequest(profileEndpoint, accessToken); | |
var extraData = new Dictionary<string, string>(); | |
extraData.Add("accesstoken", accessToken); | |
try | |
{ | |
using (WebResponse profileResponse = request.GetResponse()) | |
{ | |
using (Stream responseStream = profileResponse.GetResponseStream()) | |
{ | |
XDocument document = LoadXDocumentFromStream(responseStream); | |
extraData.AddDataIfNotEmpty(document.Root.Element("user"), "permalink"); | |
extraData.AddDataIfNotEmpty(document.Root.Element("user"), "active_email"); | |
extraData.AddDataIfNotEmpty(document.Root.Element("user"), "display_name"); | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
// At this point, the authentication is already successful. | |
// Here we are just trying to get additional data if we can. | |
// If it fails, no problem. | |
} | |
if (extraData.ContainsKey("display_name")) | |
userName = extraData["display_name"]; | |
return new AuthenticationResult( | |
isSuccessful: true, provider: this.ProviderName, providerUserId: userId, userName: userName, extraData: extraData); | |
} | |
/// <summary> | |
/// Helper method to load an XDocument from an input stream. | |
/// </summary> | |
/// <param name="stream">The input stream from which to load the document.</param> | |
/// <returns>The XML document.</returns> | |
internal static XDocument LoadXDocumentFromStream(Stream stream) | |
{ | |
const int MaxChars = 0x10000; // 64k | |
XmlReaderSettings settings = new XmlReaderSettings() | |
{ | |
MaxCharactersInDocument = MaxChars, | |
}; | |
return XDocument.Load(XmlReader.Create(stream, settings)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! :-) Got it working and XING returns the Token, but I'm wondering which kind of TokenManager I've got to pass to XING-Client:
XingClient xc = new XingClient("1bf...0", "7c...ed", ????);
AuthenticationResult rslt = xc.VerifyAuthentication(HttpContext);
Maybe you could provide a sample call to VerifyAuthentication?