0
我在我的WCF服務中覆蓋UserNamePasswordValidator,我想驗證用戶名和密碼對SQL數據庫。無論如何要將連接字符串傳遞到PasswordValidator類中嗎?我還應該提到我的課程與主要的WCF服務是分開的。如何在重寫時傳遞連接字符串UserNamePasswordValidator
public class MyCustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
//TODO: how to pass in connection string??
connectionString = "Data Source=MyDataSource;Initial Catalog=MyCatalog;User ID=MyUserName;Password=MyPassword";
// connect to database and validate crendentials...
if (!valid)
{
throw new SecurityTokenException("Access Denied.");
}
}
}
配置部分:
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MyCompany.Services.Validation.MyCustomUserNameValidator, MyCompany.Services.Validation"/>
感謝。
我應該提到MyCustomUserNameValidator是在一個單獨的dll中o主要的WCF服務。如果有一種方法可以調用主web.config,我會很滿意,但如果設置與主Web配置不匹配,則有第二個配置文件會導致其他開發人員感到困惑和沮喪。 – user1662409 2013-05-09 10:57:21
你可以使用配置爲'var config = ConfigurationManager.OpenExeConfiguration(「MyDll.dll.config」);' – Veer 2013-05-09 11:03:18
或 只需將一個app.config文件添加到DLL項目中,並確保從配置設置中讀取配置在DLL裏面。部署時,您的配置文件需要具有名稱「MyDLL.dll.config」(假設您的DLL被命名爲「MyDLL.dll」)並且與DLL位於同一文件夾中。 以下代碼應該從我的dll返回我的connectionstring: 'return ConfigurationManager.AppSettings [「DbConnection」];' – Veer 2013-05-09 11:05:46