0

I have a simple login function:

    private bool AdminIsValid(string username, string password)
    {
        bool isValid = false;
        using (var db = new AdminEntities())
        {

            var admin = db.users.FirstOrDefault(u => u.password.Trim() == password);

            if (admin != null)
            {
                if (admin.uname.Trim() == username)
                {
                    isValid = true;
                }
            }
        }
        return isValid;
    }

That I use like this:

    protected void LoginButton_Click(object sender, EventArgs e)
    {            
        if (AdminIsValid(LoginUser.UserName, LoginUser.Password))
            {
                FormsAuthentication.SetAuthCookie(LoginUser.UserName, false);
                Response.Redirect("../");
            }
            else
            {
               //"Log in data failed"
            }           
    }

It only works when the login is successful, when it fails I get this error:

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

meda
  • 45,103
  • 14
  • 92
  • 122
  • Good afternoon, maybe this [post][1] can be of assistance. [1]: http://stackoverflow.com/questions/7375857/failed-to-generate-a-user-instance-of-sql-server-due-to-a-failure-in-starting-th – mreyeros Oct 22 '13 at 19:56
  • @mreyeros I seen this already, it didnt help thanks tho – meda Oct 22 '13 at 20:20

2 Answers2

2

You have User Instance=True in your connection string, remove that. Also you should compare user name and password together, since multiple users can have a same password, you can modify your method like:

private bool AdminIsValid(string username, string password)
{
    using (var db = new AdminEntities())
    {
       return db.users.Any(u => u.password.Trim() == password &&
                                u.uname.Trim() == username);
    }
}
Habib
  • 219,104
  • 29
  • 407
  • 436
  • This make sense, I found that in `ApplicationServices` connection string which I don't use, when removing it I get error CANNOT CREATE DATBASE – meda Oct 22 '13 at 20:14
0

I fixed this by replacing all ApplicationServices connection strings with my own.

Then I ran the following command:

aspnet_regsql.exe -S DBServerName -U DBLogin -P DBPassword -A all -d DBName
meda
  • 45,103
  • 14
  • 92
  • 122