Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written below method in normal .net page. I need the same method in 3-tier architecture. I’m unable to change the below method. Please help me on this.

Split the same method in 3-tier as BOL/DAL/BLL.
C#
protected void CmdLogin_Click(object sender, EventArgs e)
        {

            _path = "LDAP://#";
            //lblerrormsg.Text = string.Empty;
            try
            {
                if (!IsAuthenticated(Convert.ToString(ConfigurationManager.AppSettings["domain"]), Login1.UserName, Login1.Password))
                {
                    return;
                }
                SqlCommand cmd = new SqlCommand("#");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@UserID", SqlDbType.NVarChar, 50).Value = Login1.UserName;
                LMS dal = new LMS();
                DataSet ds = dal.DisconectedMethod(cmd);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    Session["Authorization"] = Convert.ToString(ds.Tables[0].Rows[0]["ROLE"]);
                    Session["UserID"] = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
                    Session["EMPLOYEE_NAME"] = Convert.ToString(ds.Tables[0].Rows[0]["EMPLOYEE_NAME"]);
                    Response.Redirect("index.aspx");
                }
            }
            catch (Exception ex)
            {
                Login1.FailureText = ex.Message;
            }

        }


        private bool IsAuthenticated(string domain, string username, string pwd)
        {
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = entry.NativeObject;

                DirectorySearcher search = new DirectorySearcher(entry);

                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();

                if (null == result)
                {
                    return false;
                }

                //Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute = (string)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                Login1.FailureText = ex.Message;
            }

            return true;
        }
Posted
Updated 16-Nov-14 20:07pm
v3
Comments
Sinisa Hajnal 17-Nov-14 2:16am    
If you're unable to change it, how can you split it? Do you mean you don't know how?

1 solution

Put database access code in your DAL class (i.e. AuthenticationDB)
Put IsAuthenticated in you BLL class (i.e. AuthenticationManager)
Create User class with properties UserId, Authorisation and Employee name, return that from BLL

Then your CmdLogin_Click look like this

If (!AuthenticationManager.IsAuthenticated){
Employee e = AuthenticationManager.Login(username, pwd)
if (e == null) { // some error message
}
else
{
Session["user"] = e;

}


In authenticationManager.Login(username, pwd)
you will call
AuthenticationDB db = new AuthentiationDB(connectionString)
db.Login(username, pwd), construct Employee object from the data returned and return the object to UI layer.


If this helpsplease take time to accept the solution. Thank you.
 
Share this answer
 
Comments
v2vinoth 25-Nov-14 2:23am    
Hi I am new to 3-tier architecture. Could you please explain the code.
Sinisa Hajnal 25-Nov-14 3:37am    
Not sure what do you need. You asked about splitting your method. So I provided you with DAL class AuthenticationDB (you can call it whatever, of course) which handles direct database access. AuthenticationManager is your BusinessLogic class which serves as DAL client and provides validation and other business rules for your authentication. And finally, you have your original class (View) that now holds much less code.

You model class is User which describes the data you return from the database. It contains the properties / values that the manager gets from the DAL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900