Example #1
0
        bool ImportHttpAuthScheme(MetadataImporter importer,
                                  HttpTransportBindingElement bindingElement,
                                  PolicyConversionContext context)
        {
            var assertions  = context.GetBindingAssertions();
            var authSchemes = AuthenticationSchemes.None;

            var  httpsTransport = bindingElement as HttpsTransportBindingElement;
            bool certificate    = httpsTransport != null ?
                                  httpsTransport.RequireClientCertificate : false;

            var authElements = PolicyImportHelper.FindAssertionByNS(
                assertions, PolicyImportHelper.HttpAuthNS);

            foreach (XmlElement authElement in authElements)
            {
                assertions.Remove(authElement);

                if (certificate)
                {
                    importer.AddWarning(
                        "Invalid authentication assertion while " +
                        "using client certificate: {0}", authElement.OuterXml);
                    return(false);
                }

                switch (authElement.LocalName)
                {
                case "BasicAuthentication":
                    authSchemes |= AuthenticationSchemes.Basic;
                    break;

                case "NtlmAuthentication":
                    authSchemes |= AuthenticationSchemes.Ntlm;
                    break;

                case "DigestAuthentication":
                    authSchemes |= AuthenticationSchemes.Digest;
                    break;

                case "NegotiateAuthentication":
                    authSchemes |= AuthenticationSchemes.Negotiate;
                    break;

                default:
                    importer.AddWarning(
                        "Invalid policy assertion: {0}", authElement.OuterXml);
                    return(false);
                }
            }

            bindingElement.AuthenticationScheme = authSchemes;
            return(true);
        }