开发者

Create our own login functionality in asp.net without using default logincontrol of ASP.NET

How to Create a Login, Signup and after user Login then loggedin username will be dispalyed in every page using Label with out using login control and create userwizard

I have following Field in My Table1

ID        Username        Email id         Password
1         dobriyal        dd@d.com         ssssss
2         manish          tt@d.com         ttreter

i want to create login in sumit.aspx page using Textbox1 and textbox2 and button ...when user eneter emailid in textbox1 and password in textb开发者_开发问答ox2 then if userfind in database according to the emailid and password entered in textbox1 and textbox2 .... then loggedin username will be displayed in label1 of each page ...where i have label 1 on page ///

means .... if i have label1 on Default.aspx, myname.aspx, defaul2.aspx then in each page the label1 text would be loggedin username .....till they loggedit its session ...

How to do it using Vb.NET

Remember I dont wanna use default login & createuserwizard, login status and login name control of ASp.NET ...


Answer of your question is very broad. So instead of code i am giving you the algorithm.

1. Create a method that will check the supplied user name and password in your data layer file. E.g. bool IsAuthenticatedUser(). Inside this method run Command.ExecuteScalar. Here is query "

Declare @Count =(Select Count(*) From Login 
                Where UserName = @UserNAme and Password = @Password)
               Select @Count;  

-- Assuming u r using Sql Server 2008

2. Now in your IsAuthenticated Method check if the returned value is one then return true else false. Something like this

int count = (int)command.ExecuteScalar();
If (count ==1)
{
  return true;
}
return false;

3. On the UI check the value returned by the method. If its true redirect the user to its page. And set session. For displaying name email id on page, After the success of IsAuthenticated method, Create another method that will select your desired entities from database and fill it in datatable. Something like this

If(IsAuthenticatedUser(username, password))
  {
     Datatable dt = GetUserDetails(username,password);
     If(dt!=null)
     {
         if(dt.rows.count >0)
          {
               // here there is only one row so you can use index [0] also
               foreach(DataRow dr in dt.rows)
               {
                   Session["UserName"] = dr["UserName"].tostring();
                   Session["Email"] = dr["Email"].tostring();
               }
          }
     } 

Now on the desired page check if the both sessions are not null , then set the value at desired label.

If (Session["UserName"]!=null && Session["Email"]!=null)
{
   lblUserName.Text = "Welcome " + Session["UserName"].ToString();
   lblEmail.Text = Session["Email"].tostring();
}

**Here userName and password parameter will be the value passed from the textbox.

string username = txtUserName.text.trim();
string password = txtPassword.text.trim();

Also you to put validations before accepting input from users, as this may result in sql injection and exposing of your entire database. but for home purpose you can do this.**

See Update:

  1. For displaying name on every page Create a master page and add the two labels on that. For adding master page , from context menu Add New Item -> MasterPage. Now in source view add table as per your need. Also there is ContentPlaceholder , do not add inside that tag. That is the space to display the other child pages. in the master page load event use the label = session code to set the name. Also there add a link button , in its click event use

    Session.Abandon();

    Response.Redirect("~/Login.aspx?Logout=1");

    -> i have added a new thing ?Logout called query string , used for passing values from one page to another. Here i am using Login =1 that means I can know ia m on login page because i logged out myself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜