Gustavo Melo
Gustavo Melo

Reputation: 199

How to test a sql server connection windows authentication from .Net Project Test outside the domain

Here is the cenario:

I have an account created on Active Directory named Test.

This account have permission to read the instance of database.

I can access the data inside the domain> through SQL Server Visual Management Studio with Windows Authentication.

Now the problem:

How can I outside> the domain will access this data with a .NET Project Test?

I put this in my 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>

But i'm still getting this error:

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

The last and not least, yes i do have both SQL and windows authentication mode enabled.

Upvotes: 2

Views: 9853

Answers (2)

negEntropy
negEntropy

Reputation: 329

I use the following snippet when I am outside my AD Domain:

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;
}

Then I use it like this:

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

Upvotes: 1

Adil
Adil

Reputation: 148150

If SQL server is outside the domain then you have to provide the IP and port of server like this

Change in connectionstring

From

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

To

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

In the above statement 212.22.231.11 server which has Database hosted in SQL Server. and 1433 is port exposed by SQL Server

Upvotes: 2

Related Questions