C# 如何在LogReceiverService(NLog)中启用安全性

C# 如何在LogReceiverService(NLog)中启用安全性,c#,wcf,wcf-security,nlog,C#,Wcf,Wcf Security,Nlog,我必须建立一个集中的日志存储库,并决定挂载一个WCF服务,实现NLog的LogReceiverService(通过wsHttpBinding)。我在下面找到了一个工作示例(在上有一个工作代码) 好的,现在问题来了:我想给这个WCF服务添加一些安全性,通过HTTPS公开它,也许还可以添加一个。我之前已经编程了这种身份验证,所以我知道怎么做,只是我不知道如何在NLog中编程。我是否应该修改NLog调用WCF方法的类?我简直想象不出怎么做。对于如何实现此功能的任何想法,我都非常感激。最终我能够做到这一

我必须建立一个集中的日志存储库,并决定挂载一个WCF服务,实现NLog的LogReceiverService(通过wsHttpBinding)。我在下面找到了一个工作示例(在上有一个工作代码)


好的,现在问题来了:我想给这个WCF服务添加一些安全性,通过HTTPS公开它,也许还可以添加一个。我之前已经编程了这种身份验证,所以我知道怎么做,只是我不知道如何在NLog中编程。我是否应该修改NLog调用WCF方法的类?我简直想象不出怎么做。对于如何实现此功能的任何想法,我都非常感激。

最终我能够做到这一点

让我告诉您,我能够配置所需的行为:)

首先,我们按如下方式配置服务器:

WCFService的web.config的System.ServiceModel配置为:



我终于做到了这一点

让我告诉您,我能够配置所需的行为:)

首先,我们按如下方式配置服务器:

WCFService的web.config的System.ServiceModel配置为:



我终于做到了这一点

让我告诉您,我能够配置所需的行为:)

首先,我们按如下方式配置服务器:

WCFService的web.config的System.ServiceModel配置为:



我终于做到了这一点

让我告诉您,我能够配置所需的行为:)

首先,我们按如下方式配置服务器:

WCFService的web.config的System.ServiceModel配置为:


public class UsernameValidator : UserNamePasswordValidator
{
    private const string UserName = "your_username_here";
    private const string Password = "your_password_here";

    public override void Validate(string userName, string password)
    {
        // validate arguments
        if (string.IsNullOrEmpty(userName))
            throw new ArgumentNullException("userName");
        if (string.IsNullOrEmpty(password))
            throw new ArgumentNullException("password");

        //
        // Nombre de usuario y contraseñas hardcodeados por seguridad
        //
        if (!userName.Equals(UserName) || !password.Equals(Password))
            throw new SecurityTokenException("Nombre de usuario o contraseña no válidos para consumir este servicio");
    }
}
// we assume that this class is created in NLog.CustomExtendedService namespace

[Target("LogReceiverSecureService")]
public class LogReceiverSecureService : NLog.Targets.LogReceiverWebServiceTarget
{
    /// <summary>
    /// Gets or sets the UserName of the service when it's authentication is set to UserName
    /// </summary>
    /// <value>The name of the endpoint configuration.</value>
    public string ServiceUsername { get; set; } 

    /// <summary>
    /// Gets or sets de Password of the service when it's authentication is set to UserName
    /// </summary>
    public string ServicePassword { get; set; }

    /// <summary>
    /// Creates a new instance of WcfLogReceiverClient.
    /// 
    /// We make override over this method to allow the authentication
    /// </summary>
    /// <returns></returns>
    protected override NLog.LogReceiverService.WcfLogReceiverClient CreateWcfLogReceiverClient()
    {
        var client = base.CreateWcfLogReceiverClient();
        if (client.ClientCredentials != null)
        {
            //
            // You could use the config file configuration (this example) or you could hard-code it (if you do not want to expose the credentials)
            //
            client.ClientCredentials.UserName.UserName = this.ServiceUsername;
            client.ClientCredentials.UserName.Password = this.ServicePassword;
        }
        return client;
    }
}