у меня есть проблема, когда я пытаюсь использовать WCF с безопасностью. appid = {appguid} "< /p>
В приложении на стороне сервера (на ПК с Windows 11) я создал хост с этим: < /p>
ServiceHost shRet = null;
NetTcpBinding binding = new NetTcpBinding();
binding.Name = "CodeNetTcpBinding1";
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.Security.Mode = SecurityMode.TransportWithMessageCredentials;
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
binding.TransferMode = TransferMode.Streamed;
address = string.Format("net.tcp://{0}:{1}/ServiceTest", "PCNAME", "49152");
baseAddress = new Uri(address);
shRet = new ServiceHost(typeof(WCFService.GrossingService), baseAddress);
shRet.Description.Behaviors.Add(new System.ServiceModel.Description.ServiceMetadataBehavior() { HttpGetEnabled = false, HttpsGetEnabled = false });
shRet.Credentials.ServiceCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.Root, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "PCNAME");
shRet.Credentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.Root, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "PCNAME");
shRet.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
shRet.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new Validation.MyUserNameValidator();
shRet.AddServiceEndpoint(typeof(WcfService.IMilewatchGrossingService), binding, "");
shRet.AddServiceEndpoint(typeof(WcfService.IMilewatchGrossingDeviceService), binding, "");
shRet.AddServiceEndpoint(typeof(System.ServiceModel.Description.IMetadataExchange), mexTcpBinding, "mex");
< /code>
Проверка является следующим классом: < /p>
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
namespace Validation
{
public class MyUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName != "TestUser" || password != "ABC")
throw new SecurityTokenException("User or password not valid");
}
}
}
< /code>
, в то время как часть на стороне клиента следующая: < /p>
System.ServiceModel.NetTcpBinding binding = new System.ServiceModel.NetTcpBinding();
binding.Name = "CodeNetTcpBinding1";
binding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
binding.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
binding.TransferMode = System.ServiceModel.TransferMode.Streamed;
address = string.Format("net.tcp://{0}:{1}/ServiceTest", "PCNAME", "49152");
epAddress = new System.ServiceModel.EndpointAddress(address);
Program.ServiceClient = new ServiceClient(binding, epAddress);
Program.ServiceClient.ClientCredentials.ClientCertificate.SetCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine, System.Security.Cryptography.X509Certificates.StoreName.Root, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "PCNAME");
Program.ServiceClient.ClientCredentials.UserName.UserName = "TestUser";
Program.ServiceClient.ClientCredentials.UserName.Password = "ABC";
try
{
Program.ServiceClient.Open();
Program.ServiceClient.InnerChannel.OperationTimeout = new TimeSpan(0, 0, 30);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
break;
< /code>
Кроме того, я взял автопогнированный сертификат, созданный с сервера, и я установил его в корне клиента. System.serviceModel.Channels.ServiceChannel,
не может использоваться для общения, потому что оно находится в неисправном состоянии. < /P>
< /blockquote>
Подробнее здесь: https://stackoverflow.com/questions/795 ... redentials
Связь с WCF с ClientCredentials ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
VS 2022 Добавить ссылку на службу, не генерирующую ClientCredentials для .Net Framework
Anonymous » » в форуме C# - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-
-
-
VS 2022 Добавить ссылку на службу, не генерирующую ClientCredentials для .Net Framework
Anonymous » » в форуме C# - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-