User Name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Tom Nowak said:
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.
 
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.
 
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
 
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.
 
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>
 
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)
 
Is that actually a GoTo statement? :^)

You need to use RedirectFromLoginPage to set the username.

FormsAuthentication.RedirectFromLoginPage(txtLogin.Value, False)

Greg
 
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
 
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.
 
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
 
Back
Top