how to display username in a lable after he logins

Posted by Saritha.rajeshkumar under C# on 4/22/2011 | Points: 10 | Views : 36157 | Status : [Member] | Replies : 9
what is the code to display username in another page, after he logins in 3tier arcitecture.
i.e after user logins i should get Hi, username in another page.




Responses

Posted by: Vforvijay on: 4/22/2011 [Member] Starter | Points: 25

Up
0
Down
hi..
After user login ,then save user name in session variable..By using session variable you can display username in all pages.
Session("username") = txtUser.Text

http://vbdotnetaddict.blogspot.com

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 4/22/2011 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
Hi,

In the page where the user enters his username store that value in a session in this way

Session("username") = txtUserName.Text;


and in your home page use a label lo display that username.

lblUsername.Text = Session("username");


This is the simple way .

In case of a 3-tire architecture pass this Session variable as an input parameter to Business Access Layer(BAL) and reference it back to UI part.

Thanks,
Radha Srikanth

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 4/22/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi saritha . .

u do this by cookies also .

create Cookies
HttpCookie username = new HttpCookie("username");
wrote query and fetch user name and create cookies . .

username.Value = rd.GetValue(6).ToString();
Response.Cookies.Add(username);


Print Cookies

lblusers.Text = HttpContext.Current.Request.Cookies["username"].Value.ToString();

Best Regard's
Prabhakar

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 4/22/2011 [Member] Starter | Points: 25

Up
0
Down
But i feel more than cookies , session are preferred... :)


Best Regards,
Rohan Laghate

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gokul on: 4/25/2011 [Member] Starter | Points: 25

Up
0
Down
You can use Session to hold the username , since session values remains althrough the application is on. Try it .




Thanks and Regards
GokulNithy

Thanks and Regards,
GokulNath Nithy.

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 4/25/2011 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
As i mentioned earlier sessions is one of the option. But ,if you want to place the value of user name only at a single page why carry that variable through out the application?

In this case using Cookies is not a bad idea, as we are dealing with only user name itself . If we want to store password then there might be some security issues.

Thanks,
Radha Srikanth

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lakn2 on: 5/13/2011 [Member] Starter | Points: 25

Up
0
Down
use session you can show username in lables

in the login page give session["username"]=textbox1.text;


in your home page access label.text =sessio["username"].ToString();

Thanks&Regards
LakshmiNarayana Nalluri.

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandhyab on: 11/30/2012 [Member] Starter | Points: 25

Up
0
Down
To display CurrentUser i.e logged in User Name:
Label1.Text=HttpContext.Current.User.Identity.Name.ToString();
If yo uou want to display Like Welcome,After Sign in Try like this.
Use this code where you want to display like Welcome,"UserName":
<asp:LoginView runat="server" ID="LoginView1">

<AnonymousTemplate>
Welcome Guest,
<asp:LoginStatus runat="server" ID="LoginStatus1" />
</AnonymousTemplate>
<LoggedInTemplate>
Welcome
<asp:LoginName runat="server" ID="LoginName1" />
,
<asp:LoginStatus runat="server" ID="LoginStatus1" />
</LoggedInTemplate>
</asp:LoginView>


And, LogIn using 3-tier:
LogIn.aspx:

<asp:Login ID="Login1" runat="server" BackColor="#EFF3FB" BorderColor="#B5C7DE" BorderPadding="4"

BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="1em"
ForeColor="#333333" OnAuthenticate="Login1_Authenticate">
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
<TextBoxStyle Font-Size="0.8em" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
</asp:Login>

Code Behind:
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using DotNetFundaBAL;

public partial class LogIn : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool isAuthenticated = false;
ArticleBAL b = new ArticleBAL();
string UserName = Login1.UserName;
string Password = Login1.Password;
object obj = b.LogIn(UserName, Password);
if (obj != null)
{
isAuthenticated = true;
}


if (isAuthenticated)
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);


}
}
}

In DAL:
 public object LogIn(string UserName, string Password)

{
using (SqlConnection conn = new SqlConnection(_constr))
{
string sql = "SELECT CustomerId from Registration WHERE UserName = @UserName AND Password = @Password";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
conn.Open();
object obj = cmd.ExecuteScalar();
conn.Close();
return obj;


}
}
}


In BAL:
public object LogIn(string UserName, string Password)

{
ArticleDAL i = new ArticleDAL();
return i.LogIn(UserName, Password);
}


web.config:Add this
<authentication mode="Forms">

<forms defaultUrl="~/Forums.aspx" loginUrl="~/LogIn.aspx" slidingExpiration="true" timeout="20"/>
</authentication>

Thanks & regards

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bageshkumarbagi on: 12/3/2012 [Member] Starter | Points: 25

Up
0
Down
use session.

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied

Saritha.rajeshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response