Connecting to a remote sql server

Discussion in 'ASP .Net' started by Frank Schumacher, Jan 27, 2005.

  1. Hi NG,

    I try to connect to a remote MS-SQL-Server 2000 from an ASP.NET
    application. Here's how I do it:

    private void ConnectToDatabase(string host, string dataBaseName, string
    userID, string password)
    {
    string connectionString;

    connectionString = "Initial Catalog=" + dataBaseName + ";";
    connectionString += "Data Source=" + host + ";";
    connectionString += "Integrated Security=SSPI;";
    connectionString += "User ID=" + userID + ";";
    connectionString += "Password=" + password + ";";

    _myConnection = new SqlConnection();
    _myConnection.ConnectionString = connectionString;
    try
    {
    _myConnection.Open();
    }
    catch (Exception e)
    {
    throw e;
    }
    }

    If I use this module in a windows-gui c# project, it works fine. But in
    an ASP.NET application I get the error: Login failed for user
    'domainname\computername$'
    I have double checked User ID and Password in the connetion string, but
    they were correct.

    Any ideas on that?

    Thanks,
    Frank
    Frank Schumacher, Jan 27, 2005
    #1
    1. Advertising

  2. I see from your code that you are using SQL Server's integrated security. It
    means that SQL Server uses the account under which the application is
    running, and not the user ID and password quoted in the connection string,
    to decide whether it will let it in or not.

    ASP.NET applications run under a local machine account named ASPNET. You
    need to give this account the necessary rights. If your database were
    located on the same computer as the executable then I'd advise you to make
    the account a member of the Administrators" group. But since your database
    is located on a remote computer, I'm not sure what would be the best thing
    to do. Maybe impersonation?

    Viele Grüße nach Leipzig,
    Michal

    --
    Michal Boleslav Mechura


    "Frank Schumacher" <-leipzig.de> wrote in message
    news:%...
    > Hi NG,
    >
    > I try to connect to a remote MS-SQL-Server 2000 from an ASP.NET
    > application. Here's how I do it:
    >
    > private void ConnectToDatabase(string host, string dataBaseName, string
    > userID, string password)
    > {
    > string connectionString;
    >
    > connectionString = "Initial Catalog=" + dataBaseName + ";";
    > connectionString += "Data Source=" + host + ";";
    > connectionString += "Integrated Security=SSPI;";
    > connectionString += "User ID=" + userID + ";";
    > connectionString += "Password=" + password + ";";
    >
    > _myConnection = new SqlConnection();
    > _myConnection.ConnectionString = connectionString;
    > try
    > {
    > _myConnection.Open();
    > }
    > catch (Exception e)
    > {
    > throw e;
    > }
    > }
    >
    > If I use this module in a windows-gui c# project, it works fine. But in an
    > ASP.NET application I get the error: Login failed for user
    > 'domainname\computername$'
    > I have double checked User ID and Password in the connetion string, but
    > they were correct.
    >
    > Any ideas on that?
    >
    > Thanks,
    > Frank
    Michal Boleslav Mechura, Jan 27, 2005
    #2
    1. Advertising

  3. Frank Schumacher

    bruce barker Guest

    because you set integrated security on in the connection string, the
    user/password setting are ignored, they are for sqlserver's standard
    security. the webconfig supports setting the nt user account and password,
    see <identity impersonate="true" username='domain\user" password="password"
    />

    -- bruce (sqlwork.com)


    "Frank Schumacher" <-leipzig.de> wrote in message
    news:%...
    | Hi NG,
    |
    | I try to connect to a remote MS-SQL-Server 2000 from an ASP.NET
    | application. Here's how I do it:
    |
    | private void ConnectToDatabase(string host, string dataBaseName, string
    | userID, string password)
    | {
    | string connectionString;
    |
    | connectionString = "Initial Catalog=" + dataBaseName + ";";
    | connectionString += "Data Source=" + host + ";";
    | connectionString += "Integrated Security=SSPI;";
    | connectionString += "User ID=" + userID + ";";
    | connectionString += "Password=" + password + ";";
    |
    | _myConnection = new SqlConnection();
    | _myConnection.ConnectionString = connectionString;
    | try
    | {
    | _myConnection.Open();
    | }
    | catch (Exception e)
    | {
    | throw e;
    | }
    | }
    |
    | If I use this module in a windows-gui c# project, it works fine. But in
    | an ASP.NET application I get the error: Login failed for user
    | 'domainname\computername$'
    | I have double checked User ID and Password in the connetion string, but
    | they were correct.
    |
    | Any ideas on that?
    |
    | Thanks,
    | Frank
    bruce barker, Jan 27, 2005
    #3
  4. Thanks Bruce and Michal,
    that has done it :)

    Ciao,
    Frank

    bruce barker wrote:
    > because you set integrated security on in the connection string, the
    > user/password setting are ignored, they are for sqlserver's standard
    > security. the webconfig supports setting the nt user account and password,
    > see <identity impersonate="true" username='domain\user" password="password"
    > />
    >
    > -- bruce (sqlwork.com)
    Frank Schumacher, Jan 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    399
  2. Me LK
    Replies:
    3
    Views:
    419
    Sanka Kolitha
    Nov 4, 2007
  3. Hifni Shahzard

    Connecting to Remote SQL Server

    Hifni Shahzard, Feb 9, 2005, in forum: ASP .Net Security
    Replies:
    4
    Views:
    161
    Milos Milovanovic
    Feb 17, 2005
  4. Wesley Chen
    Replies:
    0
    Views:
    219
    Wesley Chen
    Apr 7, 2009
  5. Wesley Chen
    Replies:
    9
    Views:
    241
    Sean O'Halpin
    Apr 14, 2009
Loading...

Share This Page