đầu tiên chúng ta cần phải thực hiện hành vi và thanh tra viên ở phía khách hàng để gửi chính để xác thực client:
class AuthenticationBehaviour : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
//AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
//endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
}
public void Validate(ServiceEndpoint endpoint)
{
}
class AuthenticationMessageInspector : IClientMessageInspector
{
private const string HeaderKey = "Authentication";
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
if (Session.MachineId == 0)
{
Session.MachineId = LicenseGenerator.GenerateLicense();
}
request.Headers.Add(MessageHeader.CreateHeader(HeaderKey, string.Empty, Session.MachineId));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
bây giờ chúng ta cần phải thực hiện hành vi và thanh tra trong máy chủ bên (dịch vụ WCF) để kiểm tra mọi yêu cầu thực hiện và trích xuất các tiêu đề sau đó xác nhận điều đó:
public class AuthenticationBehaviour : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
//AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
//clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{
AuthenticationMessageInspector inspector = new AuthenticationMessageInspector();
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
//Console.WriteLine("Dispatcher Applied!");
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
public class AuthenticationMessageInspector : IDispatchMessageInspector
{
#region Members
private string conStr = "", commStr = "";
public IDbConnection Connection { get; set; }
public IDbCommand Command { get; set; }
public IDataReader Reader { get; set; }
#endregion
private const string HeaderKey = "Authentication";
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
//Console.WriteLine("recieved Request! ");
int headerIndex = request.Headers.FindHeader(HeaderKey, string.Empty);
if (headerIndex < 0 || string.IsNullOrEmpty(request.Headers.GetHeader<String>(headerIndex)))
{
throw (new Exception("Access Denied!\n"));
return null;
}
bool valid = Validate(request.Headers.GetHeader<String>(headerIndex));
if (!valid)
{
Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex) + " and Access Denied!\n");
throw (new Exception("Access Denied!\n" + request.Headers.GetHeader<String>(headerIndex) + " License Number is not athourized! "));
}
if (headerIndex != -1)
{
Console.WriteLine("recieved Request! From " + request.Headers.GetHeader<String>(headerIndex));
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
bây giờ cho phép đăng ký hành vi:
_routingHost.Description.Endpoints[0].Behaviors.Add(new Gateway.Controllers.AuthenticationBehaviour());
_routingHost.Open();
đó là cảm ơn.
Xem http://stackoverflow.com/questions/964433/how-to-add-a-custom-header-to-every-wcf-calls để tự động chuyển dữ liệu dưới dạng tiêu đề. Tôi không biết làm thế nào để tự động kiểm tra nó trên máy chủ, tuy nhiên, nhưng tôi mong đợi có một cái móc. – Rup