Solved

"Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection"

Posted on 2004-10-27
1,800 Views
Last Modified: 2008-02-07
I wrote a SQL connection program. It works in connecting to the SQL server in the console application program. But when I copied the same code to a web form under button_click event handling, system reported:
"Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection"

Why it works in C# console application while not working in the asp.net web form? They use the same SqlConnection:
new SqlConnection("Initial Catalog=DataMasters; Data Source=Abcdefg ;Integrated Security=SSPI");

And the security has been set up as: Authentication: SQL Server and Winows.
On the startup service account (start and run sql server in the following account) (in security configuration), system administrator selected an account name and a password already though (instead of using system account).

thanks a million,
Scott
0
Question by:heyday2004
    15 Comments
     
    LVL 17

    Expert Comment

    by:AerosSaga
    do you have Identity Ipersonate=true in your web.config?

    Aeros
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    <identity impersonate="true" />
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    what account is it running under in IIS?  
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    That is the account that will need to have a sql server account to access the db
    0
     
    LVL 8

    Expert Comment

    by:boulder_bum
    Is there an ASPNET account set up for the database?

    You may either need to set up an account, or configure credentials to be sent properly accross the network (I believe the ASP.NET worker process only has anonymous access to network resources by default).

    http://support.microsoft.com/default.aspx?scid=kb;en-us;307002
    0
     
    LVL 9

    Expert Comment

    by:riyasjef
    try this

    new SqlConnection("Initial Catalog=DataMasters;uid=sa;pwd=; Data Source=Abcdefg ;Integrated Security=SSPI");

    RJ
    0
     

    Author Comment

    by:heyday2004
    Thanks. Here are some parts that I have doubt with in my web.config:
    <authentication mode="Windows" />
    <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20" />
    ...

    When I added <identity impersonate="true" />, there was another error:
    Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

    Also, how to check what account is running under IIS?

    Thanks.
    0
     
    LVL 17

    Accepted Solution

    by:
    ok you need to create an Active Directory Windows account for the application to run under or use an existing one.
    Go to your IIS manager and click on the site properites and then look under the directory security tab.  There it wil let you choose
    the account the application runs under set it to your desired account with the sql server account and all will be well.

    Aeros
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    don't worry about that top stuff its fine.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    currently its running with anonymous access as the default process.  That is why the username is null, and you are being denied.
    0
     
    LVL 25

    Assisted Solution

    by:nauman_ahmed
    1. Create a SQL Server user using SQL Server Enterprise Manager. Make sure that your SQL server supports both the Windows and SQL Authentication.

    2. Use the follwoing connection string:

    Data Source=192.168.0.10,1433;Network Library=DBMSSOCN;Initial Catalog=northwind;User ID=SQL_USER;Password=SQL_PASSWORD;"

    In your console application, you didnt have any problem as the application was accessing the SQL server under the privileges of user account that you are logged in. However, in asp.net web application, ASPNET user account do not have high privileges and if you do not provide the user account info in the connection string, it tries to access the server using ASPNET account that by default do not have permission to access the server.

    HTH, Nauman.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 28

    Assisted Solution

    by:mmarinov
    Hi heyday2004,

    the connection is open under the account it is executable. when you create a web site the defaul settings are - enabled for the anonimous user
    when you use Windows Authentication you can not retrieve ( .net also :) ) which is the current user that is logged into the machine
    so when you try to connect to the SQL Server .net pass (null ) user

    to remove that you have to remove anonymus access for your web site
    1. Open Internet Information Service console
    2. RIght click on your virtual directory/web site
    3. go to Directory Security Tab
    4. Click on Edit button in the  Authentication and access control
    5. uncheck the checkbox Enable Anonymous Access

    Regards!
    B..M
    0
     

    Author Comment

    by:heyday2004
    Thanks  a lot, all! I created a account and solved the problem. Thanks again.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now