我这次用窗口服务调用wcf不起作用ClientCredentials

时间:2019-07-16 06:47:30

标签: c# wcf

新用户添加自己的ClientCredentials并访问文件,这一次将引发异常文件无法访问,但用户可以将此文件应用于\ pareshweb1.in.paresh.com \ ADPTest \ PareshTest.txt

请给我其他方式来申请证书并访问我的文件。

//this code call to wcf hosted in window services

  //use ChannelFactory
            string url = "net.tcp://localhost:8733/Service1";

            NetTcpBinding binding = new NetTcpBinding()
            {
                OpenTimeout = TimeSpan.FromMinutes(10),
                SendTimeout = TimeSpan.FromMinutes(10),
                CloseTimeout = TimeSpan.FromMinutes(10),
                ReceiveTimeout = TimeSpan.MaxValue,
                MaxBufferSize = int.MaxValue / 8,
                MaxReceivedMessageSize = int.MaxValue / 8,
                MaxBufferPoolSize = int.MaxValue,

                ListenBacklog = 100,
                MaxConnections = 100,
            };


            //binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

            ChannelFactory<IService1> factory = new ChannelFactory<IService1>(
                binding, url);

            var defaultCredentials = factory.Endpoint.Behaviors.Find<ClientCredentials>();
            factory.Endpoint.Behaviors.Remove(defaultCredentials);

            ClientCredentials loginCredentials = new ClientCredentials();
            loginCredentials.UserName.UserName = "ram";
            loginCredentials.UserName.Password = "test@123";
            factory.Endpoint.Behaviors.Add(loginCredentials);

            //inject endpoint behavior
            //factory.Endpoint.Behaviors.Add(new SimpleEndpointBehavior());

            IService1 client = factory.CreateChannel();

通过wcf方法逻辑

public class Service1 : IService1
    {
        private string logPath = @"C:\TEMP\TestLog.txt";

        //[OperationBehavior(Impersonation = ImpersonationOption.Required)]
        public string GetData(int value)
        {
            try
            {
                WriteInfoLog("File Write Start");
                File.AppendAllText(@"\\pareshweb1.in.paresh.com\ADPTest\PareshTest.txt","file Write Sucess");

                WriteInfoLog("File Write End");

            }
            catch(Exception ex)
            {
                WriteErrorLog(ex);
            }

            return string.Format("You entered: {0}", value);
        }

        #region Log Write

        private void WriteInfoLog(string str)
        {
            try
            {

                File.AppendAllText(logPath, str + Environment.NewLine + DateTime.Now + Environment.NewLine);
            }
            catch (Exception ex)
            {

                File.AppendAllText(logPath, ex.Message + Environment.NewLine + DateTime.Now + Environment.NewLine);
            }

        }

        private void WriteErrorLog(Exception exception)
        {
            try
            {
                File.AppendAllText(logPath, exception.Message + Environment.NewLine + DateTime.Now + Environment.NewLine);

            }
            catch (Exception ex)
            {

                File.AppendAllText(logPath, ex.Message + Environment.NewLine + DateTime.Now + Environment.NewLine);
            }

        }

        #endregion
    }

这次致电wcf服务时引发以下错误

拒绝访问路径'\ pareshweb1.in.paresh.com \ ADPTest \ PareshTest.txt'。

1 个答案:

答案 0 :(得分:0)

网络共享目录需要凭据,而不是承载WCF服务的帐户。我们可以通过将文件写入本地文件目录来排除此问题。除了计算机(服务器主机)已存储访问网络共享目录的凭据外,我认为我们无法指定访问网络中目录的凭据。
在Windows中,它位于“控制面板”>“凭据管理器”中。
此外,以上的客户端凭据是为服务器验证网络共享目录帐户以外的客户端提供的。
随时让我知道问题是否仍然存在。