簡體   English   中英

使用存儲庫數據庫的WCF自定義用戶名和密碼驗證程序c#

[英]WCF Custom Username and Password Validator using a Repository Database c#

我有一個以編程方式配置的WCF服務 - 沒有XML配置文件。 我正在嘗試使用存儲庫數據庫表來實現自定義用戶名密碼驗證以進行驗證。 我不希望在客戶端 - 服務器上要求Windows用戶名/密碼。 我正在使用NetTCPBinding。

我知道如何創建CustomValidator,以及如何將其分配給ServiceBehavior。 我的問題是,當服務實例化時,我不知道如何為驗證器設置RepositoryConnection字符串(我不知道該機制是如何工作的。)我的驗證器的這個屬性是我需要設置的 - 編程 - 請記住我沒有使用任何XML CONFIG文件。 public string RepositoryConnection {get; 組;}

我知道我的NetTCPBinding需要更改,因為這是我的第一次WCF安全嘗試,歡迎任何評論,我會嘗試更新問題以提供任何進一步的信息。 我在下面發布了相關的代碼部分。

public static NetTcpBinding SetNetTCPBinding(string name, string ns)
{
    NetTcpBinding binding = new NetTcpBinding(); //SecurityMode.None);
    binding.Security.Mode = SecurityMode.Transport;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
    binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    binding.Security.Transport.ProtectionLevel = ProtectionLevel.None;
}


public class ServiceUserNamePasswordValidator : UserNamePasswordValidator
{
    **public string RepositoryConnection { get; set;}**

    public override void Validate(string userName, string password)
    {
            try
            {
                if (string.IsNullOrEmpty(userName) || password == null)
                {
                    //throw new ArgumentNullException();
                    throw new FaultException("Username cannot be null or empty; Password cannot be null and should not be empty");
                }
                IGenericDataRepository<MyAppDBData.Users> repositorySingle = new GenericDataRepository<MyAppDBData.Users>(RepositoryConnection);
                MyAppDBData.Users USER = new MyAppDBData.Users();

                USER = repositorySingle.GetSingle(d => d.Name.ToLower() == userName.ToLower() && d.Password == password);

                if (USER == null)
                {
                    // This throws an informative fault to the client.
                    throw new FaultException("Unknown Username or Incorrect Password");
                }
            }
            finally{
            }
   }
}

這是我尋找答案的地方 - 除了漱口。

WCF身份驗證:自定義用戶名和密碼驗證程序asp.net

不執行WCF自定義用戶名和密碼驗證

WCF netTCPBinding

WCF TransportCredentialOnly不發送用戶名和密碼

使用NetTcpBinding在WCF中進行Windows身份驗證/加密

我在這里采樣了

WebServiceHost host = new ServiceHost(typeof(CloudService), httpUri);
host.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
    .UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
host.Description.Behaviors.Find<ServiceCredentials>().UserNameAuthentication
    .CustomUserNamePasswordValidator = new ServiceUserNamePasswordValidator(repositoryConnectionString);

因此,您可以將存儲庫名稱/ connstring作為構造函數參數傳遞,或者在將其分配給主機屬性之前初始化驗證程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM