2012-05-04 38 views
2

這裏從外面.NET項目測試SQL Server連接的Windows身份驗證是cenario:如何測試域

我有一個名爲測試在Active Directory中創建一個帳戶。

此帳戶有權限讀取數據庫的實例。

我可以使用Windows身份驗證通過SQL Server Visual Management Studio訪問域>內的數據。

現在的問題:

我怎樣才能外面>域名,將與.NET項目測試訪問這些數據?

我把這個在我的app.config:

<connectionStrings> 
    <add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/> 
</connectionStrings> 
<system.web> 
    <identity impersonate="true" userName="domain\user" password="pass"/> 
</system.web> 

但我仍然收到此錯誤:

Login failed for user 'x'. The user is not associated with a trusted SQL Server connection.

最後,而不是最不重要的,是的,我確實有SQL和窗口認證模式啓用。

回答

2

如果SQL服務器是域之外,那麼你必須要提供這樣

更改IP和服務器端口的ConnectionString

<add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/> 

<add name="CRM" connectionString="Data Source=212.22.231.11,1433; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/> 

在上面的語句中,212.22.231.11服務器中有數據庫託管在SQL S中erver。和1433端口由SQL Server暴露

+0

不'數據源= 212.22.231.11,1433'? – zimdanen

+0

@zimdanen我仍然繼續收到**用戶登錄失敗。用戶未與可信的SQL Server連接相關聯。** –

+0

您是否刪除了impersonate標記,並且還告訴我您是SQL Server用戶名/密碼身份驗證(如果不創建用戶並使用該用戶與SQL Server管理工作室連接)。如果您成功登錄使用該用戶名傳遞連接字符串,請參閱http://www.connectionstrings.com/sql-server-2005另請參閱http://www.dreamincode.net/forums/topic/155164-connecting-to-a -remote-sql-server-via-internet-ip-address /和搜索sql服務器錯誤代碼 – Adil

1

我用下面的代碼片段時,我爲我的AD域之外:

using System.DirectoryServices; 
using System.Diagnostics; 
using System.Management; 
using System.DirectoryServices.AccountManagement; 

public bool IsAuthenticated(String domain, String username, String pwd) 
{ 
    // this is a query of the students credentials 
    try 
    { 
     //Bind to the native AdsObject to force authentication.     
     String domainAndUsername = domain + "\\" + username; 
     DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd); 
     Object obj = entry.NativeObject; 
     DirectorySearcher search = new DirectorySearcher(entry); 
     search.Filter = "(SAMAccountName=" + username + ")"; 
     search.PropertiesToLoad.Add("cn"); 
     SearchResult result = search.FindOne(); 
     if (null == result) 
     { 
      return false; 
     } 
     //Update the new path to the user in the directory. 
     _path = result.Path; 
     _filterAttribute = (String)result.Properties["cn"][0]; 
    } 
    catch (Exception ex){} 
    return true; 
} 

然後我用這樣的:

var adAuth = new LdapAuthentication(@"LDAP://snip.edu");    
bool auth = adAuth.IsAuthenticated("snip", "username","password" 
if (auth) 
{ 
    // do something} 
} 
+0

但我真正需要的是測試與Sql Server的連接,我想如果連接可以在域外打開則爲true。 –

+0

請勿使用集成安全性? – TomTom