From 02dbbd2c7b09816c9c379b92ff9acce28d297ef1 Mon Sep 17 00:00:00 2001 From: Anders Abel Date: Tue, 17 Feb 2015 14:28:06 +0100 Subject: [PATCH] Changed config for SampleOwinApp to use in code config. - Now possible to remove all traces of AuthServices and System.IdentityModel from web.config. - Updated Federation ctor to use IOptions instead of Options. --- Kentor.AuthServices/Federation.cs | 2 +- .../App_Start/Startup.Auth.cs | 92 ++++++++++++++++++- .../SampleOwinApplication.csproj | 1 + SampleOwinApplication/Web.config | 26 ------ 4 files changed, 92 insertions(+), 29 deletions(-) diff --git a/Kentor.AuthServices/Federation.cs b/Kentor.AuthServices/Federation.cs index efefeca66..d710380ab 100644 --- a/Kentor.AuthServices/Federation.cs +++ b/Kentor.AuthServices/Federation.cs @@ -43,7 +43,7 @@ public Federation(FederationElement config, IOptions options) /// Options to pass on to created IdentityProvider /// instances and register identity providers in. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "sp")] - public Federation(Uri metadataUrl, bool allowUnsolicitedAuthnResponse, Options options) + public Federation(Uri metadataUrl, bool allowUnsolicitedAuthnResponse, IOptions options) { Init(metadataUrl, allowUnsolicitedAuthnResponse, options); } diff --git a/SampleOwinApplication/App_Start/Startup.Auth.cs b/SampleOwinApplication/App_Start/Startup.Auth.cs index cd19528eb..d0f416587 100644 --- a/SampleOwinApplication/App_Start/Startup.Auth.cs +++ b/SampleOwinApplication/App_Start/Startup.Auth.cs @@ -7,6 +7,14 @@ using Owin; using SampleOwinApplication.Models; using Kentor.AuthServices.Owin; +using Kentor.AuthServices.Configuration; +using System.IdentityModel.Metadata; +using System.Globalization; +using Kentor.AuthServices.Metadata; +using Kentor.AuthServices; +using Kentor.AuthServices.WebSso; +using System.Security.Cryptography.X509Certificates; +using System.Web.Hosting; namespace SampleOwinApplication { @@ -35,10 +43,90 @@ public void ConfigureAuth(IAppBuilder app) validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } - }); + }); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); - app.UseKentorAuthServicesAuthentication(new KentorAuthServicesAuthenticationOptions(true)); + app.UseKentorAuthServicesAuthentication(CreateAuthServicesOptions()); + } + + private static KentorAuthServicesAuthenticationOptions CreateAuthServicesOptions() + { + var spOptions = CreateSPOptions(); + var authServicesOptions = new KentorAuthServicesAuthenticationOptions(false) + { + SPOptions = spOptions + }; + + authServicesOptions.IdentityProviders.Add( + new IdentityProvider( + new EntityId("http://stubidp.kentor.se/Metadata"), spOptions) + { + AllowUnsolicitedAuthnResponse = true, + Binding = Saml2BindingType.HttpRedirect, + SingleSignOnServiceUrl = new Uri("http://stubidp.kentor.se"), + SigningKey = new X509Certificate2( + HostingEnvironment.MapPath("~/App_Data/Kentor.AuthServices.StubIdp.pfx")) + .PublicKey.Key + }); + + // It's enough to just create the federation and associate it + // with the options. The federation will load the metadata and + // update the options with any identity providers found. + new Federation(new Uri("http://localhost:52071/Federation"), true, authServicesOptions); + + return authServicesOptions; + } + + private static SPOptions CreateSPOptions() + { + var swedish = CultureInfo.GetCultureInfo("sv-se"); + + var organization = new Organization(); + organization.Names.Add(new LocalizedName("Kentor", swedish)); + organization.DisplayNames.Add(new LocalizedName("Kentor IT AB", swedish)); + organization.Urls.Add(new LocalizedUri(new Uri("http://www.kentor.se"), swedish)); + + var spOptions = new SPOptions + { + EntityId = new EntityId("http://localhost:57294/AuthServices"), + ReturnUrl = new Uri("http://localhost:57294/Account/ExternalLoginCallback"), + DiscoveryServiceUrl = new Uri("http://localhost:52071/DiscoveryService"), + Organization = organization + }; + + var techContact = new ContactPerson + { + Type = ContactType.Technical + }; + techContact.EmailAddresses.Add("authservices@example.com"); + spOptions.Contacts.Add(techContact); + + var supportContact = new ContactPerson + { + Type = ContactType.Support + }; + supportContact.EmailAddresses.Add("support@example.com"); + spOptions.Contacts.Add(supportContact); + + var attributeConsumingService = new AttributeConsumingService("AuthServices") + { + IsDefault = true, + }; + + attributeConsumingService.RequestedAttributes.Add( + new RequestedAttribute("urn:someName") + { + FriendlyName = "Some Name", + IsRequired = true, + NameFormat = RequestedAttribute.AttributeNameFormatUri + }); + + attributeConsumingService.RequestedAttributes.Add( + new RequestedAttribute("Minimal")); + + spOptions.AttributeConsumingServices.Add(attributeConsumingService); + + return spOptions; } } } \ No newline at end of file diff --git a/SampleOwinApplication/SampleOwinApplication.csproj b/SampleOwinApplication/SampleOwinApplication.csproj index 6fbcf2a70..6790a08eb 100644 --- a/SampleOwinApplication/SampleOwinApplication.csproj +++ b/SampleOwinApplication/SampleOwinApplication.csproj @@ -87,6 +87,7 @@ + diff --git a/SampleOwinApplication/Web.config b/SampleOwinApplication/Web.config index bf0adbe0d..0ff59e453 100644 --- a/SampleOwinApplication/Web.config +++ b/SampleOwinApplication/Web.config @@ -7,9 +7,6 @@
-
-
-
@@ -30,29 +27,6 @@ - - - - - - - - - - - - - - - - - - - -