/// <summary> /// Performs OpenID discovery according to the YADIS protocol and returns details about the returned provider. /// </summary> public static IdentityProviderYadisDocument DiscoverIdentityProvider(string identifier) { Uri identifierUri = NormalizeIdentifier(identifier); IdentityProviderYadisDocument openIdEndpoint = null; XmlReader yadisReader = null; HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(identifierUri); webRequest.Accept = YadisConstants.YadisMimeType; using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { // // The MIME type may contain encoding information after a semicolon, we are only interested in the actual content type. // string contentType = webResponse.ContentType.Split(';')[0]; // // YADIS 1.0: The response MUST be one of: // if (contentType.Equals(YadisConstants.YadisMimeType, StringComparison.OrdinalIgnoreCase)) { // // A document of MIME media type, application/xrds+xml. // yadisReader = XmlReader.Create(webResponse.GetResponseStream()); } else if (!string.IsNullOrEmpty(webResponse.GetResponseHeader(YadisConstants.XrdsLocationHeader))) { // // HTTP response-headers (with or without a document) that include an X-XRDS-Location response-header. // yadisReader = XmlReader.Create(webResponse.GetResponseHeader(YadisConstants.XrdsLocationHeader)); } else { // // An HTML document with a <head> element that includes a <meta> element with http-equiv attribute, X-XRDS-Location // using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { string yadisUrl = GetYadisUrlFromHtml(sr.ReadToEnd()); if (!string.IsNullOrEmpty(yadisUrl)) { yadisReader = XmlReader.Create(yadisUrl); } } } if (yadisReader != null) { openIdEndpoint = new IdentityProviderYadisDocument(yadisReader); } } return(openIdEndpoint); }
static void Main(string[] args) { // // This is the OpenID identifier of the identity provider. // This could be changed to be any OpenID provider. // const string siteIdentifier = "myopenid.com"; const string providerName = "MyOpenID"; Console.WriteLine("Attempting OpenID discovery for identifier '{0}'", siteIdentifier); try { IdentityProviderYadisDocument discoveryDocument = OpenIdDiscovery.DiscoverIdentityProvider(siteIdentifier); if (discoveryDocument != null && !string.IsNullOrEmpty(discoveryDocument.OpenIdEndpoint)) { Console.WriteLine("Successfully discovered OpenID sign-in address: '{0}'.", discoveryDocument.OpenIdEndpoint); Console.WriteLine("Provider supports attribute exchange? {0}", discoveryDocument.SupportsAttributeExchange); // // OpenID discovery was successful. Add the discovered IdentityProvider to ACS. // ManagementService svc = ManagementServiceHelper.CreateManagementServiceClient(); svc.DeleteIdentityProviderIfExists(providerName); svc.SaveChangesBatch(); IdentityProvider idp = svc.CreateOpenIdIdentityProvider(providerName, discoveryDocument.OpenIdEndpoint); // // Associate this identity provider with all relying parties. // svc.AssociateIdentityProvidersWithRelyingParties(new[] { idp }, svc.RelyingParties.Where(rp => rp.Name != "AccessControlManagement")); svc.SaveChangesBatch(); Console.WriteLine("\nSuccessfully added identity provider '{0}' to ACS.", providerName); Console.WriteLine("Press ENTER to continue....\n"); Console.ReadLine(); // // Deleting the issuer also causes the identity provider and any associated objects to be deleted. // svc.DeleteObject(idp.Issuer); svc.SaveChanges(); Console.WriteLine("\nSuccessfully deleted identity provider."); } else { Console.WriteLine("OpenID discovery failed. Ensure that the identifier is valid."); } } catch (Exception e) { Console.WriteLine("Exception was thrown: " + e.ToString()); } Console.WriteLine("Done. Press ENTER to continue....\n"); Console.ReadLine(); }