Trouble accessing db while using ASP.Net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: BFlaherty (BFlaherty_2003_at_yahoo.com)
Date: 07/26/04


Date: 25 Jul 2004 21:59:15 -0700

Group,
 

I read Jeff Prosise's article: "An Introductory Guide to Building and
Deploying More Secure Sites with ASP.NET and IIS, Part 2" as well as
part 1. I think that articles are great. I had no problems running
the example code in the first article, but I am having problems with
the example as outlined in Figure 4. Specifically, I seem to be
having troubles opening a connection to the database, and that's why I
am posting to this group. When I try to open the connection
("connection.Open ();"), I am receiving this exception:

"Exception Message: Login failed for user 'FormUser'. Reason: Not
associated with a trusted SQL Server connection.
Stack Trace: at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean&
isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString
options, Boolean& isInTransaction) at
System.Data.SqlClient.SqlConnection.Open() at
ASP.LoginPage_aspx.CustomAuthenticate(String username, String
password)
Invalid login
User name: jeff"
[end of exception]

The user "FormUser" was created by using this script:

EXEC sp_addlogin 'FormUser', 'formuser', 'WebLogin'
EXEC sp_grantlogin 'BILLDESKTOP\FormUser'
EXEC sp_grantdbaccess 'BILLDESKTOP\FormUser', 'formuser'

I have attached a copy of my LoginPage.aspx as well as the associated
web.config file. Can you tell me what is going wrong here? I have
tried to do research on this error, and as far as I can see, I have
taken care of all of the isses. Also, if I turn impersonation on, I
get the same error, but complaining about a differant user.

I am using MSDE and Windows XP Pro.

Also, I did send a similar EMail to the author, but I am sure that he
gets a lot of bulk email from confused people like me, so I hope that
one of you could help me.

Thanks,
Bill

_________________________________________
login.aspx:
<%@ Import NameSpace="System.Data.SqlClient" %>

<html>
  <body>
    <h1>Please Log In</h1>
    <hr>
    <form runat="server">
      <table cellpadding="8">
        <tr>
          <td>
            User Name:
          </td>
          <td>
            <asp:TextBox ID="UserName" RunAt="server" />
          </td>
        </tr>
        <tr>
          <td>
            Password:
          </td>
          <td>
            <asp:TextBox ID="Password" TextMode="password"
              RunAt="server" />
          </td>
        </tr>
        <tr>
          <td>
            <asp:Button Text="Log In" OnClick="OnLogIn"
              RunAt="server" />
          </td>
          <td>
            <asp:CheckBox Text="Keep me signed in" ID="Persistent"
              RunAt="server" />
          </td>
        </tr>
      </table>
    </form>
    <hr>
    <h3><asp:Label ID="Output" RunAt="server" /></h3>
  </body>
</html>

<script language="C#" runat="server">
  void OnLogIn (Object sender, EventArgs e)
  {
      if (CustomAuthenticate (UserName.Text, Password.Text))
          FormsAuthentication.RedirectFromLoginPage (UserName.Text,
              Persistent.Checked);
      else
          Output.Text += "Invalid login" + "<br>" + "User name: " +
UserName.Text;
  }

  bool CustomAuthenticate (string username, string password)
  {
      SqlConnection connection = new SqlConnection
          ("server=BILLDESKTOP\\JMSDEV1;database=weblogin;Integrated
Security=SSPI;uid=FormUser;pwd=formuser");

      try {
          connection.Open (); //this is where the excpetion is thrown

          StringBuilder builder = new StringBuilder ();
          builder.Append ("select count (*) from users " +
              "where username = \'");
          builder.Append (username);
          builder.Append ("\' and cast (rtrim (password) as " +
              "varbinary) = cast (\'");
          builder.Append (password);
          builder.Append ("\' as varbinary)");

          SqlCommand command = new SqlCommand (builder.ToString (),
              connection);

          int count = (int) command.ExecuteScalar ();

          return (count > 0);
      }
      catch (SqlException se) {//this is where the exception is caught
          Output.Text += "Exception Message: " + se.Message + "<BR>";
          Output.Text += "Stack Trace: " + se.StackTrace + "<BR>";
          return false;
      }
      finally {
          connection.Close ();
      }
  }
</script>

____________________________
web.config:

<configuration>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="LoginPage.aspx" />
    </authentication>
  </system.web>
</configuration>



Relevant Pages

  • Re: Mobile 6 connection to SQL Express
    ... You should be handling the SqlException in that try::catch not just top level Exception. ... Where ex is of type SqlCeException. ... In VS2005 i've created a new data source using assistant, the resulting string connection is ... But if i try to make a "manual" connection to database i just got an SqlException and no more info, i've debbuged it, but i dont have more information, why the dataset connects and shows info, and when i try to use the same string connection i can't open the connection? ...
    (microsoft.public.dotnet.framework.compactframework)
  • sql select to file using java
    ... Developer) the accounts are selected. ... Connection connection = null; ... public ExtractAccountInfor(String dbstring, String user, String ... } catch (Exception x){ ...
    (comp.databases.oracle.misc)
  • Re: Databind Error???
    ... wrapper object looking just like the Microsoft command object and even named ... I'm not opening a connection, ... connection string to the command object and even disposing it and setting it ... An unhandled exception occurred during the execution of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SqlDataAdapter.Fill exception in Release but not Debug Mode
    ... connection string was different in the connection object than what I was ... the exception seems to be occuring when I query ... Private m_ConnString As String ... startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: OT: Effective complaint; a long ramble
    ... connection suddenly disappeared; I got a static message on my TV saying ... His name was Jeff, and I ... to the VP of Customer Service for COMCAST outlining the problem, ... She also had a recording of my exchange ...
    (alt.support.stop-smoking)