How to implement WebServiceHost Authentication?

后端 未结 3 1316
萌比男神i
萌比男神i 2020-12-21 18:59

I\'m aware that the authentication on the webservicehost class does not adhere fully to authentication standards (returns 403 forbidden rather than prompting for another set

3条回答
  •  半阙折子戏
    2020-12-21 20:03

    You can write a custom WebServiceHost by inheriting from it and change some default parameters like below.

    The only change in your code would be

    Dim varWebService = New AuthenticatedWebServiceHost(GetType(MyWebService), New Uri("http://0.0.0.0/"))
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IdentityModel;
    using System.IdentityModel.Selectors;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.ServiceModel.Security;
    using System.ServiceModel.Description;
    
    namespace StackOverflow
    {
        public class AuthenticatedWebServiceHost : WebServiceHost
        {
            public AuthenticatedWebServiceHost(Type type, Uri url)
            {
                IDictionary desc = null;
                base.InitializeDescription(type, new UriSchemeKeyedCollection());
                base.CreateDescription(out desc);
                var val = desc.Values.First();
    
                WebHttpBinding binding = new WebHttpBinding();
                binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    
                base.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
                base.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomUserNamePasswordValidator();
    
                base.AddServiceEndpoint(val.ContractType, binding, url);
            }
    
            //Possible next question:
            //"How can I get the name of the authenticated user?"
            public static string UserName
            {
                get
                {
                    if (OperationContext.Current == null) return null;
                    if (OperationContext.Current.ServiceSecurityContext == null) return null;
                    if (OperationContext.Current.ServiceSecurityContext.PrimaryIdentity == null) return null;
                    return OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name;
                }
            }
    
    
    
            public class CustomUserNamePasswordValidator : UserNamePasswordValidator
            {
                public override void Validate(string userName, string password)
                {
                    //Your logic to validate username/password
                    if (userName != password)
                        throw new SecurityAccessDeniedException();
                }
            }
        }
    }
    

提交回复
热议问题