Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to test a sql server connection windows authentication from .Net Project Test outside the domain
- <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="domainuser" password="pass"/>
- </system.web>
- <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"/>
- 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}
- }
Add Comment
Please, Sign In to add comment