saving the login name as current user identity in

2019-06-05 09:05发布

问题:

So I'm making a asp.net login. I want the login name that people use to match an id in my SQL database. So that I can retrieve their information. But currently when I use the code below, from which I get the name of the computer I am currently on. However I would like the user Identity to be what they write in the username textbox at the login screen.

If HttpContext.Current.User.Identity.IsAuthenticated Then
    Dim userName As String = HttpContext.Current.User.Identity.Name
    Response.Write(userName)
End If

So I looked for it on net and I think it might have something to do with my web.config file. As I'm totally new to asp.net I wouldn't know. However here's a part of my Web.config file.

<authentication mode="Forms">
    <forms name=".ASPXFORMSAUTH"
        loginUrl="Login.aspx"
        protection="All"
        timeout="30"
        path="/" />
</authentication>
<authorization>
    <deny users="?" />
</authorization>

so any ideas? :)

回答1:

set login name using

FormsAuthentication.RedirectFromLoginPage(UserName.Text, RememberMe.Checked);

and retrive using

User.Identity.Name

reference links, bytes.com and asp.net tutorial



回答2:

Use the ViewStates to maintain the Login state of the user.