By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

User Name

P: n/a
I am writing a webapp in which a user is required to enter a login id and
password on a login form. I have forms authenticaion coded in my web.config.
Once the user is logged in, I want to use the login id in other forms of the
app. I will eventually save a record to a SQL database, and I want the login
id to be automatically entered in a field on a form other than the login
page. Help.
Nov 18 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Session variables.

Eliyahu

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
I am writing a webapp in which a user is required to enter a login id and
password on a login form. I have forms authenticaion coded in my web.config. Once the user is logged in, I want to use the login id in other forms of the app. I will eventually save a record to a SQL database, and I want the login id to be automatically entered in a field on a form other than the login
page. Help.

Nov 18 '05 #2

P: n/a
How do I define and use them?

"Eliyahu Goldin" wrote:
Session variables.

Eliyahu

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
I am writing a webapp in which a user is required to enter a login id and
password on a login form. I have forms authenticaion coded in my

web.config.
Once the user is logged in, I want to use the login id in other forms of

the
app. I will eventually save a record to a SQL database, and I want the

login
id to be automatically entered in a field on a form other than the login
page. Help.


Nov 18 '05 #3

P: n/a
Tom Nowak <To******@discussions.microsoft.com> typed:
How do I define and use them?


After the authentication process insert the value in a session variable

Session["UserId"] = UserId;

To use the variable

if(Session["UserId"] != null)
int UserId = Int32.Parse(Session["UserId"]);

If you need more info about your authenticated user, you could consider to
create your own user object, populate it after the authentication process
and than save it in a session variable. For example:

//user is authenticated

UserInfo user = new UserInfo();
user.Id = UserId;
user.Name = Name;
user.Email = Email;

Session["CurrentUser"] = user;

Than, when you need to retrieve this information ......

if(Session["CurrentUser"] != null)
UserInfo user = (UserInfo)Session["CurrentUser"];

int Id = user.Id;
string Name = user.Name;
string Email = user.Email;

.....and so on.

If you need to recover only the name of the current authenticated user, you
could use the Current.User.Identity.Name.

--
Davide Vernole
MVP ASP/ASP.NET
Microsoft Certified Solution Developer
Nov 18 '05 #4

P: n/a
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:AF**********************************@microsof t.com:
I want to use the login id in other forms of the
app.


You can use user.indentity.name to retrieve the name of the logged in user.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #5

P: n/a
When I try to display the value of user.identity.name, this value is blank.
After the user enters a valid id and password, the value is still blank.
Help.
"Lucas Tam" wrote:
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:AF**********************************@microsof t.com:
I want to use the login id in other forms of the
app.


You can use user.indentity.name to retrieve the name of the logged in user.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #6

P: n/a
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:E1**********************************@microsof t.com:
When I try to display the value of user.identity.name, this value is
blank. After the user enters a valid id and password, the value is
still blank. Help.


Oh I think you need to assign the name when you do the FormAuthentication.
There is a parameter you can set the name.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 18 '05 #7

P: n/a
The value will be blank if using Forms authentication until after you are
authenticated.

Do you have the whole Forms authentication structure setup and running
correctly?

After you think you are logged in call this: User.Identity.IsAuthenticated.
That will tell you if you really got authenticated or not.

If User.Identity.IsAuthenticated Then
Dim username as string = User.identity.name
End If

If User.Identity.IsAuthenticated is not returning true then you havn't
probably setup Forms authentication.

Do you have this (or something similar) in your web.config?

<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>

In your login.aspx to you have login button that calls:

RedirectFromLoginPage

It is in this method that you set what the username will be, so that you can
read it back using User.Identity.Name.

HTH,
Greg
"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
When I try to display the value of user.identity.name, this value is
blank.
After the user enters a valid id and password, the value is still blank.
Help.
"Lucas Tam" wrote:
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:AF**********************************@microsof t.com:
> I want to use the login id in other forms of the
> app.


You can use user.indentity.name to retrieve the name of the logged in
user.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 18 '05 #8

P: n/a
Heres the code from the login button:

Dim con As SqlConnection

con = New SqlConnection("data source=ten;initial
catalog=fbpoolSQL;trusted_connection=yes;")

con.Open()
Dim drd1 As System.Data.SqlClient.SqlDataReader
drd1 = StatusDataReader(con)

Do While drd1.Read

If drd1.GetString(1) = txtLogin.Value And _
drd1.GetString(2) = txtPwd.Value Then

Me.lblMessage.Text = "Successful"
GoTo sucrtn

End If

Loop

Me.lblMessage.Text = "Login ID or Password not valid."
txtLogin.Value = ""
txtPwd.Value = ""
GoTo endrtn

sucrtn:

Response.Redirect("choices.aspx")

endrtn:

End Sub

Heres part of my web.config:

<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
"Greg Burns" wrote:
The value will be blank if using Forms authentication until after you are
authenticated.

Do you have the whole Forms authentication structure setup and running
correctly?

After you think you are logged in call this: User.Identity.IsAuthenticated.
That will tell you if you really got authenticated or not.

If User.Identity.IsAuthenticated Then
Dim username as string = User.identity.name
End If

If User.Identity.IsAuthenticated is not returning true then you havn't
probably setup Forms authentication.

Do you have this (or something similar) in your web.config?

<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>

In your login.aspx to you have login button that calls:

RedirectFromLoginPage

It is in this method that you set what the username will be, so that you can
read it back using User.Identity.Name.

HTH,
Greg
"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
When I try to display the value of user.identity.name, this value is
blank.
After the user enters a valid id and password, the value is still blank.
Help.
"Lucas Tam" wrote:
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:AF**********************************@microsof t.com:

> I want to use the login id in other forms of the
> app.

You can use user.indentity.name to retrieve the name of the logged in
user.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/


Nov 18 '05 #9

P: n/a
=?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
news:45**********************************@microsof t.com:
Heres the code from the login button:

Dim con As SqlConnection

con = New SqlConnection("data source=ten;initial
catalog=fbpoolSQL;trusted_connection=yes;")

con.Open()
Dim drd1 As System.Data.SqlClient.SqlDataReader
drd1 = StatusDataReader(con)

Do While drd1.Read
Wow... your code is pretty bad. Just do a "SELECT FROM TABLE WHERE
USERNAME = NAME AND PASSWORD = PASSWORD to retrieve ONE record. There's
no need to loop through all the records.
If drd1.GetString(1) = txtLogin.Value And _
drd1.GetString(2) = txtPwd.Value Then

Me.lblMessage.Text = "Successful"
GoTo sucrtn
Gotos?! Don't use Gotos! Gotos create spagetti code ; )
End If

Loop

Me.lblMessage.Text = "Login ID or Password not valid."
txtLogin.Value = ""
txtPwd.Value = ""
GoTo endrtn

sucrtn:

Response.Redirect("choices.aspx")
You should be using the FormAuthentication class instead.

i.e. FormsAuthentication.RedirectFromLoginPage(txtUser. Text,
chkPersistLogin.Checked)

endrtn:

End Sub

Heres part of my web.config:

Nov 18 '05 #10

P: n/a
Is that actually a GoTo statement? :^)

You need to use RedirectFromLoginPage to set the username.

FormsAuthentication.RedirectFromLoginPage(txtLogin .Value, False)

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Heres the code from the login button:

Dim con As SqlConnection

con = New SqlConnection("data source=ten;initial
catalog=fbpoolSQL;trusted_connection=yes;")

con.Open()
Dim drd1 As System.Data.SqlClient.SqlDataReader
drd1 = StatusDataReader(con)

Do While drd1.Read

If drd1.GetString(1) = txtLogin.Value And _
drd1.GetString(2) = txtPwd.Value Then

Me.lblMessage.Text = "Successful"
GoTo sucrtn

End If

Loop

Me.lblMessage.Text = "Login ID or Password not valid."
txtLogin.Value = ""
txtPwd.Value = ""
GoTo endrtn

sucrtn:

Response.Redirect("choices.aspx")

endrtn:

End Sub

Heres part of my web.config:

<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
"Greg Burns" wrote:
The value will be blank if using Forms authentication until after you are
authenticated.

Do you have the whole Forms authentication structure setup and running
correctly?

After you think you are logged in call this:
User.Identity.IsAuthenticated.
That will tell you if you really got authenticated or not.

If User.Identity.IsAuthenticated Then
Dim username as string = User.identity.name
End If

If User.Identity.IsAuthenticated is not returning true then you havn't
probably setup Forms authentication.

Do you have this (or something similar) in your web.config?

<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>

In your login.aspx to you have login button that calls:

RedirectFromLoginPage

It is in this method that you set what the username will be, so that you
can
read it back using User.Identity.Name.

HTH,
Greg
"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
> When I try to display the value of user.identity.name, this value is
> blank.
> After the user enters a valid id and password, the value is still
> blank.
> Help.
>
>
> "Lucas Tam" wrote:
>
>> =?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote in
>> news:AF**********************************@microsof t.com:
>>
>> > I want to use the login id in other forms of the
>> > app.
>>
>> You can use user.indentity.name to retrieve the name of the logged in
>> user.
>>
>> --
>> Lucas Tam (RE********@rogers.com)
>> Please delete "REMOVE" from the e-mail address when replying.
>> http://members.ebay.com/aboutme/coolspot18/
>>


Nov 18 '05 #11

P: n/a
My thoughts exactly.

Greg
Wow... your code is pretty bad.

Nov 18 '05 #12

P: n/a
dude!

Imports System.Web.Security

You might consider doing some research on forms authentication. Buy a book
(or three). That is what I did.

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I try to use the statement below, but get an error. Thanks for the help.

"Greg Burns" wrote:
Is that actually a GoTo statement? :^)

You need to use RedirectFromLoginPage to set the username.

FormsAuthentication.RedirectFromLoginPage(txtLogin .Value, False)

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
> Heres the code from the login button:
>
> Dim con As SqlConnection
>
> con = New SqlConnection("data source=ten;initial
> catalog=fbpoolSQL;trusted_connection=yes;")
>
> con.Open()
> Dim drd1 As System.Data.SqlClient.SqlDataReader
> drd1 = StatusDataReader(con)
>
> Do While drd1.Read
>
> If drd1.GetString(1) = txtLogin.Value And _
> drd1.GetString(2) = txtPwd.Value Then
>
> Me.lblMessage.Text = "Successful"
> GoTo sucrtn
>
> End If
>
> Loop
>
> Me.lblMessage.Text = "Login ID or Password not valid."
> txtLogin.Value = ""
> txtPwd.Value = ""
> GoTo endrtn
>
> sucrtn:
>
> Response.Redirect("choices.aspx")
>
> endrtn:
>
> End Sub
>
> Heres part of my web.config:
>
> <authentication mode="Forms">
> <forms loginUrl="login.aspx" />
> </authentication>
> <authorization>
> <deny users="?" />
> </authorization>
>
>
> "Greg Burns" wrote:
>
>> The value will be blank if using Forms authentication until after you
>> are
>> authenticated.
>>
>> Do you have the whole Forms authentication structure setup and running
>> correctly?
>>
>> After you think you are logged in call this:
>> User.Identity.IsAuthenticated.
>> That will tell you if you really got authenticated or not.
>>
>> If User.Identity.IsAuthenticated Then
>> Dim username as string = User.identity.name
>> End If
>>
>> If User.Identity.IsAuthenticated is not returning true then you havn't
>> probably setup Forms authentication.
>>
>> Do you have this (or something similar) in your web.config?
>>
>> <authentication mode="Forms">
>> <forms loginUrl="login.aspx" />
>> </authentication>
>>
>> In your login.aspx to you have login button that calls:
>>
>> RedirectFromLoginPage
>>
>> It is in this method that you set what the username will be, so that
>> you
>> can
>> read it back using User.Identity.Name.
>>
>> HTH,
>> Greg
>>
>>
>> "Tom Nowak" <To******@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.com...
>> > When I try to display the value of user.identity.name, this value is
>> > blank.
>> > After the user enters a valid id and password, the value is still
>> > blank.
>> > Help.
>> >
>> >
>> > "Lucas Tam" wrote:
>> >
>> >> =?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote
>> >> in
>> >> news:AF**********************************@microsof t.com:
>> >>
>> >> > I want to use the login id in other forms of the
>> >> > app.
>> >>
>> >> You can use user.indentity.name to retrieve the name of the logged
>> >> in
>> >> user.
>> >>
>> >> --
>> >> Lucas Tam (RE********@rogers.com)
>> >> Please delete "REMOVE" from the e-mail address when replying.
>> >> http://members.ebay.com/aboutme/coolspot18/
>> >>
>>
>>
>>


Nov 18 '05 #13

P: n/a
Sorry for being so un-knowledgable. I add the Imports statement, and use the
FormsAuthentication.RedirectFromLoginPage(txtLogin .Value, False) in the
Login Button code. I have it designed so that if the user id and password
are correct, the progem redirects the user to another page. Once I am in the
other page, I want the userid displayed in a label. How do I call up the
userid in this new page?

Sorry for the ignorance, but you really have been a big help. I have
already changed the code for finding a record.

Can you recommend a good book regarding this subject?

Thanks again.

"Greg Burns" wrote:
dude!

Imports System.Web.Security

You might consider doing some research on forms authentication. Buy a book
(or three). That is what I did.

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
I try to use the statement below, but get an error. Thanks for the help.

"Greg Burns" wrote:
Is that actually a GoTo statement? :^)

You need to use RedirectFromLoginPage to set the username.

FormsAuthentication.RedirectFromLoginPage(txtLogin .Value, False)

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
> Heres the code from the login button:
>
> Dim con As SqlConnection
>
> con = New SqlConnection("data source=ten;initial
> catalog=fbpoolSQL;trusted_connection=yes;")
>
> con.Open()
> Dim drd1 As System.Data.SqlClient.SqlDataReader
> drd1 = StatusDataReader(con)
>
> Do While drd1.Read
>
> If drd1.GetString(1) = txtLogin.Value And _
> drd1.GetString(2) = txtPwd.Value Then
>
> Me.lblMessage.Text = "Successful"
> GoTo sucrtn
>
> End If
>
> Loop
>
> Me.lblMessage.Text = "Login ID or Password not valid."
> txtLogin.Value = ""
> txtPwd.Value = ""
> GoTo endrtn
>
> sucrtn:
>
> Response.Redirect("choices.aspx")
>
> endrtn:
>
> End Sub
>
> Heres part of my web.config:
>
> <authentication mode="Forms">
> <forms loginUrl="login.aspx" />
> </authentication>
> <authorization>
> <deny users="?" />
> </authorization>
>
>
> "Greg Burns" wrote:
>
>> The value will be blank if using Forms authentication until after you
>> are
>> authenticated.
>>
>> Do you have the whole Forms authentication structure setup and running
>> correctly?
>>
>> After you think you are logged in call this:
>> User.Identity.IsAuthenticated.
>> That will tell you if you really got authenticated or not.
>>
>> If User.Identity.IsAuthenticated Then
>> Dim username as string = User.identity.name
>> End If
>>
>> If User.Identity.IsAuthenticated is not returning true then you havn't
>> probably setup Forms authentication.
>>
>> Do you have this (or something similar) in your web.config?
>>
>> <authentication mode="Forms">
>> <forms loginUrl="login.aspx" />
>> </authentication>
>>
>> In your login.aspx to you have login button that calls:
>>
>> RedirectFromLoginPage
>>
>> It is in this method that you set what the username will be, so that
>> you
>> can
>> read it back using User.Identity.Name.
>>
>> HTH,
>> Greg
>>
>>
>> "Tom Nowak" <To******@discussions.microsoft.com> wrote in message
>> news:E1**********************************@microsof t.com...
>> > When I try to display the value of user.identity.name, this value is
>> > blank.
>> > After the user enters a valid id and password, the value is still
>> > blank.
>> > Help.
>> >
>> >
>> > "Lucas Tam" wrote:
>> >
>> >> =?Utf-8?B?VG9tIE5vd2Fr?= <To******@discussions.microsoft.com> wrote
>> >> in
>> >> news:AF**********************************@microsof t.com:
>> >>
>> >> > I want to use the login id in other forms of the
>> >> > app.
>> >>
>> >> You can use user.indentity.name to retrieve the name of the logged
>> >> in
>> >> user.
>> >>
>> >> --
>> >> Lucas Tam (RE********@rogers.com)
>> >> Please delete "REMOVE" from the e-mail address when replying.
>> >> http://members.ebay.com/aboutme/coolspot18/
>> >>
>>
>>
>>


Nov 18 '05 #14

P: n/a
> Can you recommend a good book regarding this subject?

ASP.NET Unleashed, Second Edition
by Stephen Walther

http://www.amazon.com/exec/obidos/tg...books&n=507846
Once I am in the
other page, I want the userid displayed in a label. How do I call up the
userid in this new page?


lblUserName.text = user.identity.name

Greg
Nov 18 '05 #15

P: n/a
You should always post a new question in a new thread.

Sounds like you are rebinding your DropDownList on every post back..

Do it like this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
...
DropDownList1.DataBind
End If

End Sub

Greg

"Tom Nowak" <To******@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
I have another question regarding a DropDown List control that I am trying
to
use. I have 15 choices in the box, and I select one, but when I press a
button on the web form, the DropDownList reverts back to the 1st choice in
the box instead of the one I choose. Any Idea?
"Greg Burns" wrote:
> Can you recommend a good book regarding this subject?


ASP.NET Unleashed, Second Edition
by Stephen Walther

http://www.amazon.com/exec/obidos/tg...books&n=507846
>Once I am in the
> other page, I want the userid displayed in a label. How do I call up
> the
> userid in this new page?


lblUserName.text = user.identity.name

Greg

Nov 18 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.