Guest User

Untitled

a guest
Sep 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. How to test a sql server connection windows authentication from .Net Project Test outside the domain
  2. <connectionStrings>
  3. <add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>
  4. </connectionStrings>
  5. <system.web>
  6. <identity impersonate="true" userName="domainuser" password="pass"/>
  7. </system.web>
  8.  
  9. <add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>
  10.  
  11. <add name="CRM" connectionString="Data Source=212.22.231.11,1433; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>
  12.  
  13. using System.DirectoryServices;
  14. using System.Diagnostics;
  15. using System.Management;
  16. using System.DirectoryServices.AccountManagement;
  17.  
  18. public bool IsAuthenticated(String domain, String username, String pwd)
  19. {
  20. // this is a query of the students credentials
  21. try
  22. {
  23. //Bind to the native AdsObject to force authentication.
  24. String domainAndUsername = domain + "\" + username;
  25. DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
  26. Object obj = entry.NativeObject;
  27. DirectorySearcher search = new DirectorySearcher(entry);
  28. search.Filter = "(SAMAccountName=" + username + ")";
  29. search.PropertiesToLoad.Add("cn");
  30. SearchResult result = search.FindOne();
  31. if (null == result)
  32. {
  33. return false;
  34. }
  35. //Update the new path to the user in the directory.
  36. _path = result.Path;
  37. _filterAttribute = (String)result.Properties["cn"][0];
  38. }
  39. catch (Exception ex){}
  40. return true;
  41. }
  42.  
  43. var adAuth = new LdapAuthentication(@"LDAP://snip.edu");
  44. bool auth = adAuth.IsAuthenticated("snip", "username","password"
  45. if (auth)
  46. {
  47. // do something}
  48. }
Add Comment
Please, Sign In to add comment