Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Do I Write A Webmethod So That My Login Page Accesses The Database To Log The Persons Details My Code For My Login Is As Follows,Need A Webmethod
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Services;
using System.Data;
using System.Xml.Linq;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Cookies["LoggedIn"]!= null && Request.Cookies["LoggedIn"].Value == "true")
        {
            Response.Redirect("Home.aspx");
        }
    }
    protected void BtnForgotPassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("ForgottenPassword.aspx");
    }
    protected void BtnRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        UseDatabase usedb = new UseDatabase(Request.PhysicalApplicationPath + "App_Data\\ProjectDatabase.accdb");
        string queryString = "SELECT * FROM [RegisteredDetails] WHERE EmailAddress = '";queryString += txtLoginEmail.Text
            + "' AND Password ='"; queryString += txtPasswordLogin.Text + "';";
        usedb.connectToDatabase();
        OleDbDataReader dbReader = usedb.ExecuteQuery(queryString);

        if (dbReader != null && dbReader.HasRows)
        {
            HttpCookie loggedInCookie = new HttpCookie("Logged In", "true");
            Response.Cookies.Add(loggedInCookie);
            Response.Redirect("Home.aspx");
        }
        else
        {
            lblError.Text = "Please enter correct Login Information" + " "+"Incorrect Username or Password";
        }
        usedb.disconnectDatabase();
    }
}
Posted
Updated 8-May-15 1:52am
v2
Comments
Richard Deeming 8-May-15 8:20am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.


You're also storing passwords in plain text, which is a terrible idea. You should only ever store a salted hash of the user's password:
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]


Also, your code provides zero protection to your application. Anyone can set a cookie called "Logged In" with a value of "true", and bypass your login screen completely.


Do yourself a favour and stop trying to roll your own security. Use one of the built-in systems instead - for example, ASP.NET Identity[^].
crucifiedtimy 8-May-15 8:50am    
hi richard thank you i appreciate the comment but what im trying to do is only for a small college assignment its not going to be published or anything i just need help with the webmethod

1 solution

Start from here - Calling ASP.Net WebMethod using jQuery AJAX[^].

If you learn how to deal with WebMethods, then you can do the rest as you have already done for Button Click.
 
Share this answer
 

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