【发布时间】:2020-04-17 22:30:18
【问题描述】:
我想创建一个 WCF 控制台自托管 - 带有证书的服务器端身份验证 - 休息服务。
我在实际调用服务时遇到了问题,因为我总是收到 401 Unauthorized 响应。
由于这是一种“单向”身份验证,其中服务向客户端标识自己,为什么我总是会作为客户端应用程序获得 401 未经授权的响应(好像客户端需要向服务标识自己以访问它的资源?)
谁能帮我查明我哪里出错了,以及如何让我的客户服务通信最终正常工作?
简单服务合同:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List<Student> GetStudentDetails();
// TODO: Add GetMethod with parameter
[OperationContract]
[WebGet(UriTemplate = "Student/{id}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Student GetStudentWithId(string id);
//TODO: add one post method here
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "Student/New", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
void NewStudent(Stream stream);
//TODO: add one post method here
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Student/NewS", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
void NewStudentS(Student stream);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Student
{
[DataMember]
public int ID
{
get;
set;
}
[DataMember]
public string Name
{
get;
set;
}
}
服务实现:
public class Service1 : IService1
{
public List<Student> GetStudentDetails()
{
return new List<Student>() { new Student() { ID = 1, Name = "Goran" } };
}
public Student GetStudentWithId(string id)
{
return new Student() { ID = Int32.Parse(id), Name = "Ticbra RanGo" };
}
public void NewStudent(Stream stream)
{
using(stream)
{
// convert Stream Data to StreamReader
StreamReader reader = new StreamReader(stream);
var dataString = reader.ReadToEnd();
Console.WriteLine(dataString);
}
}
public void NewStudentS(Student student)
{
Console.WriteLine(student.Name);
}
}
我的控制台应用程序运行该服务:
static void Main(string[] args)
{
Uri httpUrl = new Uri("https://localhost:8080/TestService");
using (WebServiceHost host = new WebServiceHost(typeof(Service1)))
{
// Create the binding.
WSHttpBinding binding = new WSHttpBinding();
binding.Name = "binding1";
binding.Security.Mode = SecurityMode.Transport;
host.AddServiceEndpoint(typeof(IService1), binding, httpUrl/*"rest"*/);
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
//Add host certificate to service wcf for identification
host.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName,
"localhost");
host.Open();
foreach (ServiceEndpoint se in host.Description.Endpoints)
Console.WriteLine("Service is host with endpoint " + se.Address);
Console.WriteLine("Host is running... Press < Enter > key to stop");
Console.ReadLine();
host.Close();
}
//Console.WriteLine("ASP.Net : " + ServiceHostingEnvironment.AspNetCompatibilityEnabled);
Console.WriteLine("Host is running... Press < Enter > key to stop");
Console.ReadLine();
}
请注意,我通过 KeyStore Explorer 创建了证书根和子证书,并将它们适当地放置在 Windows 上的个人和受信任的根证书中。
我通过 CMD 将服务器证书映射到端口 8080。
我使用的客户端是 SOAPUI,以及我的手动编码客户端。 客户代码:
WebRequest request = HttpWebRequest.Create(urlTextBox.Text);
var webResponse = request.GetResponse();
using (Stream dataStream = webResponse.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
HttpResonseTextBox.Text = responseFromServer;
}
最好的问候,并在此先感谢您
【问题讨论】:
标签: c# wcf authentication ssl-certificate webhttpbinding