Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在wcf服务中提供身份验证和授权_Wcf - Fatal编程技术网

无法在wcf服务中提供身份验证和授权

无法在wcf服务中提供身份验证和授权,wcf,Wcf,那么,您的配置在哪里呢。如果您不在配置中添加自定义验证并像下面这样添加适当的绑定,那么它将不起作用 不要期望它在创建自定义用户名验证时自动工作 将此添加到您的服务配置中。不要忘记更改名称空间和自定义有效名称 namespace POCApplication { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService

那么,您的配置在哪里呢。如果您不在配置中添加自定义验证并像下面这样添加适当的绑定,那么它将不起作用

不要期望它在创建自定义用户名验证时自动工作

将此添加到您的服务配置中。不要忘记更改名称空间和自定义有效名称

    namespace POCApplication
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {

            [OperationContract]
            string GetData(int value);

            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);


        [OperationContract]
        string UpdateEmpData(Infromation emp);


        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hi test Application ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    [DataContract]
    public class Infromation
    {

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int Age { get; set; }

        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string Gender { get; set; }
    }
}

Service1.svc.cs

namespace POCApplication
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public string SayHello(string value)
        {
            return "Hello:" + value;
        }

        public string UpdateEmpData(Infromation emp)
        {
            return string.Format("You entered: {0} , {1} , {2} , {3}",
            emp.Name, emp.Age, emp.Gender, emp.Email);
        } 
    }
}

UserAuthentication.cs

namespace POCApplication
{
    public class UserAuthentication : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
            try
            {
                if (userName == "test1" && password == "test123")
                {
                    Console.WriteLine("Authentic User");
                }
            }
            catch (Exception ex)
            {
                throw new FaultException("Unknown Username or Incorrect Password");
            }
        } 
    }
}

并相应更改您的绑定:

<serviceCredentials>  
  <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" />  
</serviceCredentials>

 <bindings>  
  <wsHttpBinding>  
      <binding name="Binding1">  
        <security mode="Message">  
          <message clientCredentialType="UserName" />  
        </security>  
      </binding>          
    </wsHttpBinding>  
  </bindings>