Login Failed for user computer \ ASPnet

Source: Internet
Author: User
Tags management studio microsoft sql server management studio sql server management sql server management studio

My database connection string is like this: Server = machinename \ sqlexpress; database = test; trusted_connection = true

It is strange that it is okay when you deploy and run the Web Service separately, but publish it to IIS. The access is problematic. Check the following information:



When you create a trusted connection from Microsoft ASP. NET to Microsoft SQL Server, you may receive the following error message:

Login Failed for user 'machinename \ aspnet'

For computers that run Internet Information Services (IIS) 6.0, you may receive the following error message:

Login Failed for user 'nt Authority \ Network Service'

NoteYou receive either of these error messages specifically when you use integrated security (when you includeIntegrated Security = sspiAttribute in a connection string ).


To resolve this issue, use one of the following methods:

Method 1Programmatically change the security context of the ASP. net worker process to a user who has the correct SQL Server permissions.

Method 2Change the default configuration of ASP. NET so that the ASP. net worker process starts and runs under the context of a user who has the correct permissions in SQL Server.

Method 3Grant the correct permissions in SQL server so that the ASPNET account (or NetworkService account, for an application that runs on IIS 6.0) has the appropriate access to the required resources.
NoteThis method will make all the web applications on the server have the corresponding right on the computer that is running SQL Server.


I use 3rd methods. I add an ASPnet user to SQL Server and grant certain permissions, because my Web Service is only used for learning purposes, therefore, the assigned permission is SysAdmin. The method is as follows:

1. Enter Microsoft SQL Server Management studio Express, right-click loings under security, and create a login

2. Create a New machinename \ ASPnet. machinename is your machine name.

3. Set server roles and other options


This error can take a variety of forms, including:

    • "Login Failed for user '(null )'"
    • "Login Failed for userDomain\Username"
    • "Login Failed for userComputer\ ASPnet"

the likely cause is that you are working with a web application and are trying to access a SQL Server. the specific error or exception that is thrown depends on whether the SQL Server is on the same computer as the Web server or on a separate computer. this problem arises because the web application is not passing proper credentials to the SQL server. in general, the solution is to:

    • If the SQL Server is on the same computer as the Web server, give the local ASPnet user login privileges on the SQL server.
    • set IIS or ASP. NET to impersonate a Windows domain user who has login privileges on the SQL server.
    • If You Want To authenticate your users in datasource, you need add to Web. config in your web-Application:

      and use "Windows Integrated Security" in your datasource configuration. As result, your users will be authenticated in you database.

    • use a connection string with an explicit user ID and password.

Change the auto generated connection string in Web. config file from
<Add name = "connectionstring" connectionstring = "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ mydatabasename. MDF;Integrated Security = true; user instance = true"Providername =" system. Data. sqlclient "/>
<Add name = "connectionstring" connectionstring = "Data Source =. \ sqlexpress; attachdbfilename = | datadirectory | \ mydatabasename. MDF;User ID = sa; Password = mysapassword; database = mydatabasename"Providername =" system. Data. sqlclient "/>


For more information, see accessing SQL Server from a Web application.




Http://msdn.microsoft.com/en-us/library/aa984220 (vs.71). aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.