Home » Visual StudioRSS

code login

I have a login page I have added the two text boxes button and label for error message  to log in I want the code for the log should note that the code to verify that the username and password is correct should also make sure that the user has not logged into by and thank you

 

10 Answers Found

 

Answer 1

You can make use of  ASP.NET login  controls:

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/security/login.aspx

http://weblogs.asp.net/scottgu/archive/2005/10/18/427754.aspx

Regards,
Isha
Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Microsoft Online Community Support

 

Answer 2

My friend I want through the language code  c# I do not want through the wizard
Thanks
 

 

Answer 3

Here you go...

/// <summary>
        /// process user login  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LoginUser(object sender, EventArgs e)
        {
            try
            {
                if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text))
                {
                    FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
                }

                else
                {
                    lblErrorMessage.Visible = true;
                }
            }

            catch (Exception ex)
            {
                lblErrorMessage.Text = ex.InnerException.ToString();
            }

            finally
            { 
                //Release resources
            }
        }


 Here is the article that explains the authetnication process with source code  http://www.asp.net/security/tutorials/validating-user-credentials-against-the-membership-user-store-cs

and Video tutorial at http://www.asp.net/security/videos/validating-users-manually

 

Answer 4

My friend let me show you the idea I have one table for users and have a page  to log  on by the two funds texts for the user  name and password  I want to make sure that username  and password are correct  agendas which she had made to employees and not by Mumbeyrchib I want the code  to do so
And thank you for your help
 

 

Answer 5

Do you mean you have some kind of backend database table which stores the usernames and password  in it. And to login  a user, you need to verify  the input values against that same database table? If so, please refer:

http://leedumond.com/blog/how-to-validate-user-input-with-values-from-a-database/

http://forums.asp.net/t/1175568.aspx

http://msdn.microsoft.com/en-us/library/s5z00s5e(vs.80).aspx

Regards,
Isha
Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Microsoft Online Community Support

 

 

Answer 6

my friend te idea of mu login  page is very simple i have two textboxes for username and password

i want to compare a table with columns that users only

        SqlConnection dbConn = new SqlConnection(@"Data source =.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\database.mdf;User Instance = true ;Integrated Security =true ");
        String mycmd = "select name,pass from users where name='" + TxtUsname + "' and pass='" + Txtpassword + "'";
        SqlCommand cmd=new SqlCommand (mycmd );
        String username;
        string pass;
        SqlDataReader dr=cmd.ExecuteReader();
        while (dr.Read)
        {
            username  = dr[0].ToString;
            pass = dr[1].ToString;

        }
        dr.Close();


        if (username == string.Empty && pass == string.Empty)
            mbox.show("error user  naem or password");
        else
            Response.Redirect("default.aspx");
        
            
    }
}


I tried with this code  but i get error  can you tell me what's error in this code 

 

Answer 7

The code  pretty much looks good. What's the exact error  you are getting?

Regards,
Isha
Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Microsoft Online Community Support

 

 

Answer 8

Please check your code  again. String vs string! And share the error  you got so it will be posible for us to help you. 

 

Answer 9

this is a fully code

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

public partial class login  : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Btlogin_Click(object sender, EventArgs e)
    {
        SqlConnection dbConn = new SqlConnection(@"Data source =.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\database.mdf;User Instance = true ;Integrated Security =true ");
        String mycmd = "select name,pass from users where name='" + TxtUsname + "' and pass='" + Txtpassword + "'";
        SqlCommand cmd=new SqlCommand (mycmd );
        String username;
        string pass;
        SqlDataReader dr=cmd.ExecuteReader();
        while (dr.Read)
        {
            username  = dr[0].ToString;
            pass = dr[1].ToString;

        }
        dr.Close();


        if (username == string.Empty && pass == string.Empty)
            mbox.show("error user  naem or password");
        else
            Response.Redirect("default.aspx");
        
            
    }
}


 the error  i get in this word (dr.read),dr[0],dr[1] and mbox

i get four errors

thanks for help me

 

Answer 10

Hi,

According to your description ,I think you can do as below:

1.Create a table which used to contains the information about UserName and Password.

UserInfo table:

UserID   UserName  PassWord

 1             name1      pass1

 2             name2      pass2

Code in .aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        UserName:<asp:TextBox ID="txb_UserName" runat="server"></asp:TextBox><br />
        PassWord:<asp:TextBox ID="txb_Password" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
         <asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
    </div>
    </form>
</body>
</html>


Code in .cs:

 protected void Button1_Click(object sender, EventArgs e)
    {
        string error;
        if (check(out error))
        {
            string LoginError;
            if (IsLogin(this.txb_UserName.Text, this.txb_Password.Text, out LoginError))
            {
                this.Label1.Visible = false;
                Response.Write("Sign in success");
            }
            else
            {
                this.Label1.Visible = true;
                this.Label1.Text = LoginError;
            }
        }
        else
        {
            this.Label1.Visible = true;
            this.Label1.Text = error;
        }
    }
    public static DataTable ExecuteDatatabel(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand();
            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
            DataTable dt = new DataTable();

            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            catch
            {
                throw;
            }
            finally
            {
                cmd.Parameters.Clear();
            }

            return dt;
        }
    }
    private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
    {

        if (conn.State != ConnectionState.Open)
            conn.Open();

        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        if (trans != null)
            cmd.Transaction = trans;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (SqlParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }
    public bool IsLogin(string UserName, string PassWord,out string LoginError)
    {
        LoginError = "";
        string str = "server=.;database=database;uid=uid;pwd=pwd;";
        //Please change the value of str as your own database.
        string CommandText = "select * from UserInfo_table where UserName=@UserName";
        SqlParameter[] parameters = {
					new SqlParameter("@UserName", SqlDbType.NVarChar,50),
					};
        parameters[0].Value = UserName;
        DataTable dt = ExecuteDatatabel(str, CommandType.Text, CommandText, parameters);
        if (dt.Rows.Count == 0)
        {
            LoginError = "UserName is not exist";
            return false;
        }
        DataRow[] dr = dt.Select("PassWord='" + PassWord + "'");
        if (dr.Length == 0)
        {
            LoginError = "PassWord is not correct";
            return false;
        }
        return true;
    }
    public bool check(out string error)
    {
        error  = "";
        if (this.txb_UserName.Text.Trim() == "")
        {
            error = "Please enter the UserName";
            return false;
        }
        if (this.txb_Password.Text.Trim() == "")
        {
            error = "Please enter the PassWord";
            return false;
        }
        return true;
    }


 Please do not forget the reference namespace:

using System.Data.SqlClient;
using System.Data;

Hope it can help you.

 

 
 
Search for  code login
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter