Ejemplo n.º 1
0
        public void ValidateSubject()
        {
            var claim   = new Claim("nameid", "homeorganization.nl:useridentifier");
            var request = SamlService.CreateAuthnRequest(claim, Guid.NewGuid().ToString(), new Uri("https://mytest.nl"));

            using (var service = new CryptographicService())
            {
                service.SignSamlRequest(request);
            }

            var requestXml = request.ToXElement();
            var ns         = XNamespace.Get("urn:oasis:names:tc:SAML:2.0:assertion");

            var element = requestXml.Element(ns + "Subject");

            Assert.IsNotNull(element);
            Assert.AreEqual(claim.Value, ((XElement)element.FirstNode).Value);
        }
        /// <summary>
        /// Begins the authentication.
        /// </summary>
        /// <param name="identityClaim">The identity claim.</param>
        /// <param name="httpListenerRequest">The HTTP listener request.</param>
        /// <param name="context">The context.</param>
        /// <returns>A presentation form.</returns>
        public IAdapterPresentation BeginAuthentication(Claim identityClaim, HttpListenerRequest httpListenerRequest, IAuthenticationContext context)
        {
            try
            {
                this.InitializeLogger();
                this.log.Debug("Enter BeginAuthentication");
                this.log.DebugFormat("context.ActivityId='{0}'; context.ContextId='{1}'; conext.Lcid={2}", context.ActivityId, context.ContextId, context.Lcid);

                string authnRequestId = $"_{context.ContextId}";
                var    authRequest    = SamlService.CreateAuthnRequest(identityClaim, authnRequestId, httpListenerRequest.Url);

                using (var cryptographicService = new CryptographicService())
                {
                    this.log.DebugFormat("Signing AuthnRequest with id {0}", authnRequestId);
                    var signedXml = cryptographicService.SignSamlRequest(authRequest);
                    return(new AuthForm(Settings.Default.SecondFactorEndpoint, signedXml));
                }
            }
            catch (Exception ex)
            {
                this.log.ErrorFormat("Error while initiating authentication:{0}", ex);
                return(new AuthFailedForm());
            }
        }