Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

iam now building a website in which i want to check whether its a authorized user and if its authorized user i want to display the name in each and every page of the website.

i tried it by setting login in master page...


Code in site master

C#
public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbdate.Text = DateTime.Now.ToLongDateString() + "/" + DateTime.Now.ToShortTimeString();
            lbt_login.Text = "Log In";
            lbt_login.Enabled = true;
        }
        else
        {
            if (Session["Ufname"] != null)
            {
                lbdate.Text = DateTime.Now.ToLongDateString() + "/" + DateTime.Now.ToShortTimeString();
                lbt_login.Text = Session["Ufname"].ToString();
                lbt_login.Enabled = false;
            }

        }
    }

    
    protected void lbt_login_Click(object sender, EventArgs e)
    {
        Response.Redirect("user_login.aspx");
    }
}


code in login page
C#
public partial class user_login : System.Web.UI.Page
{
    string str;
    dbconnection c = new dbconnection();
    int maxrecord = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tb_email.Text = "";
            tb_pwd.Text = "";
        }

    }
    protected void imgb_login_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            lb_error.Text = "";
            int flag = 0;
            str = "SELECT * FROM [user]";
            c.cmd = new System.Data.SqlClient.SqlCommand(str, c.connect());
            //c.dr = c.cmd.ExecuteReader();
            c.da = new System.Data.SqlClient.SqlDataAdapter(str, c.connect());
            c.da.Fill(c.ds);
            c.dt = c.ds.Tables[0];
            maxrecord = c.dt.Rows.Count;
            string email = "", pwd = "";
            string tbemailvalue = tb_email.Text;
            string tbpwdvalue = tb_pwd.Text;

            for(int i = 0; i < c.dt.Rows.Count; i++)
            {
                email = c.dt.Rows[i].ItemArray[4].ToString();
                pwd = c.dt.Rows[i].ItemArray[5].ToString();
                if ((tb_email.Text == email) && (tb_pwd.Text == pwd))
                {
                    flag = 1;

                    Session["Ufname"] = c.dt.Rows[i].ItemArray[2].ToString();
                    break;
                }
            }


            tb_email.Text = "";
            tb_pwd.Text = "";
            if (flag == 1)
            {
                Session["Ufname"] = tb_email.Text;
                Response.Redirect("~/Default.aspx");
                // Response.Write("authorised user.");
                //imgb_login.PostBackUrl = "~/Default.aspx";
            }
            else
            {
                Session["Ufname"] = null;
                //Label6.Text = "Invalid User,<br/>Check Your Email Id and Password.";
                lb_error.Text ="Not an authorised user.";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            //Response.Redirect("~/Default.aspx?ErrorMsg = " + ex.Message);
        }
    }
}


can any one plz help me....
Posted
Updated 25-Dec-11 19:48pm
v2

I would like to please don't use master page for login page and if you want to use the master page then make seperate master page for Default page.

If you like my solution please tell me i will provide you the solution.

Please fill free to ask me anytime .


Thanks and Regards
Suman Zalodiya
 
Share this answer
 
Comments
Ragi Gopi 26-Dec-11 1:48am    
master page is not the login page..(here its displaying home page)
i just kept a login button on master..and it navigates to a login page...
there login function is done then after the login process it is redirected to home ..then the login button should change its text to username thats my aim...
Suman Zalodiya 26-Dec-11 2:19am    
Ok then you can do one thing.

Please read my answer in Solution because i can't write code here in comment.

Thanks
Hi Ragi Gopi,

You can do one thing, On Successful login you should create Session as shown below

C++
if(compare user name and password)
{
     //if credentials matches
     Session["UName"]=txtUName.text;
     Response.Redirect("Home.aspx");
}
In the Page_Load() event of the Master page check for the session

..............Page_Load(.... ....)
C++
If(Session["UName"]==null)
       {
               Response.Redirect("user_Login.aspx");
}
else
{
           lblUName.text=Session["UName"].ToString();
}


Please check this and feel free to ask me if still you find any kind of difficulty.

Thanks and Regards
Suman Zalodiya
 
Share this answer
 
Comments
Ragi Gopi 26-Dec-11 3:51am    
what to do to clear the session while closing the browser
Suman Zalodiya 26-Dec-11 23:20pm    
Session["UName"]=null;
Suman Zalodiya 26-Dec-11 23:24pm    
Put Logout button on Master page and on click event of the Logout Button put the code

Session["UName"]=nul;

And one moer thing if you want that once you logout, user should not able to go back to those pages by Back button of browser just paste this 3 lines just before of Page_Load event of the master page

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();

Try it it's amazing...

Please revert back to me in case of any concerns i will help my best.

Thanks ans Regards
Suman Zalodiya
Ragi Gopi 3-Jan-12 6:12am    
thanks Suman....
Ragi Gopi 3-Jan-12 6:14am    
suman iam now working with iframes..

if u r not busy....can u plz look to my problem??

http://www.codeproject.com/Questions/309579/how-to-open-a-word-file-in-iframe


here is my problem..
plz help meeee
Hi,

Please Read this[^] and from above your master page code you also can remove IsPostBack condition
 
Share this answer
 
I think its a problem the way you are appending to the label.

I will tell one more approch. Try like bellow.

On the master page where you want to show the user name use like bellow

C#
<![CDATA[<%= if(!string.isEmptyOrNULL(Convert.tostring(Session["Uname"])) { response.write(Convert.tostring(Session["Uname"]); } %>]]>


Label mylabel=(Label)Master.FindControl("masterpagelabel")

If you don't want above approach. Try like in your master page load
C#
if(mylabel !=Null)
{
 mylabel.text= Convert.tostring(Session["Uname"]);
}
else
{}
 
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