Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

SQL tables come back ok but Authenticating using SQL not?

$
0
0

Dear Forum,

Following code i'm using for authentication. But i'm getting error 26 - error locating server/instance specified.However, If I call the same table in a gridview it turns up just fine? How can I get past this?

(ASP.NET 4.0 + MSSQL 2008 on a server on the local network)

I'm using following code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace RotaractWebsite
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Check if the user is already loged in or not
            if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true))
            {
                // If User is Authenticated then moved to a main page
                if (User.Identity.IsAuthenticated)
                    Response.Redirect("Home.aspx");
            }
        } //stop page load

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            Boolean blnresult;
            blnresult = false;

            // Pass UserName  and Password from login1 control to an authentication function which will check will check the user name and password from sql server.
            // Then will retrun a true or false value into blnresult variable
            blnresult = Authentication(Login1.UserName, Login1.Password);

            // If blnresult has a true value then authenticate user 
            if (blnresult == true)
            {
                // This is the actual statement which will authenticate the user
                e.Authenticated = true;
                // Store your authentication mode in session variable 
                Session["Check"] = true;
            }
            else
                // If user faild to provide valid user name and password
                e.Authenticated = false;
        }//stop login1_authenticate

        protected static Boolean Authentication(string username, string password)
        {
            string sqlstring;
            sqlstring = "Select [Account_Name], [Password] from [UserAccounts] where [Account_Name]='" + username + "' and Password ='" + password + "'";

            // create a connection with sqldatabase 
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
                         "Data Source=xxxxxxx;Initial Catalog=xxxx;User ID=xxxxx;Password=xxxxxxx;Connect Timeout=10;TrustServerCertificate=True ");

            // create a sql command which will user connection string and your select statement string
            System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring, con);

            // create a sqldatabase reader which will execute the above command to get the values from sqldatabase
            System.Data.SqlClient.SqlDataReader reader;

            // open a connection with sqldatabase
            con.Open();

            // execute sql command and store a return values in reade
            reader = comm.ExecuteReader();

            // check if reader hase any value then return true otherwise return false
            if (reader.Read())
                return true;
            else
                return false;
        }//stop authentication

    }
}


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>