How do I create a VB backcoding session variable from a Login control in Visual Web Developer 2008?

I want to create a VB backcoding session variable called 'UserName' from the UserName box on the Login control in Visual Web Developer 2008. However, whenever I create a sub (blank with no code) the login credentials are rejected. There doesn't even have to be any code---if a sub simply exists in the backcoding, then the login rejects---if I comment everything out, the login accepts. I need to be able to carry the UserName over to the Default.aspx page. How do I do this? Thanks.....
Who is Participating?
YiogiConnect With a Mentor Commented:
What kind of authentication are you using? If you are using forms authentication then you can get the username from the context.

jazjefAuthor Commented:
forms authentication is what I am using..... your suggestion seems to work, but every page I have dumps the session variable once it's set. When my login and pass was hardcoded my entire application ran fine. Now that I start putting in users with the configuration/manager tool and start using the login controls nothing works; session variables evaporate from page to page.
Hello jazjef

Could you give us an example of how you are using the Session variable?

Thank you very much,

Richard Hughes
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jazjefAuthor Commented:
It's a hardcoded login using a couple of textboxes and a session variable.

        If TextBox1.Text = "UserLogName" And TextBox2.Text = "MyPassword" Then
            Session("Authenticate") = "YES"
            Session("UserName") = TextBox1.Text
            '_____wrong login
            Exit Sub
        End If

What I really want is to use the 'login' control---it will allow me to set up user accounts etc. When I add it I can't set the session.authenticate to 'yes' and the username session variable does not persist when going to the home page. I guess it's just bad design on my part... and lack of know-how with login credentials etc.
richard_hughesConnect With a Mentor Commented:

I think you will benifit from having a look at the following links:

Let us know if they help!


Richard Hughes
Take a look at this please:

You should NOT use session variables to keep whether the user is logged on or not. You should set an authentication ticket. The way you are doing it is not forms authentication. It's what people used a decade ago! And I would certainly never use that in an application today.
jazjefAuthor Commented:
Isn't this what the aspnet mdf and the Website Configuration Tool are for?..... to allow you so set up user credentials in a database and authenticate against it? This is what I am trying to do..... I'm adding a Login Control and the aspnet mdf to my application and removing the hardcoded session variables as authentication.

The question I posted merely asks why the UserName session variable won't pass on after the Login Control does it's's not about using the session variable to authenticate. I agree that's a bad idea.

(I only used session variables while I was building the application on my local machine---I always intended on adding the authentication using the aspnet mdf and the Web Config Tool.)

jazjefAuthor Commented:
Oh..... I see what you mean now Yiogi; you are talking about how I set the session authenticate = YES after the authentication occurs.... I'm still holding the session open with a session variable and not an authentication ticket. So, my authentication is good but the means that I am using to hold the session open is bad.... correct?
Yes jazjef. You should not have to use session variables to keep if the user is authenticated. For that you have HttpContext.Current.User.Identity.IsAuthenticated. You can also get the username from HttpContext.Current.User.Identity.Name as indicated in my first post.

Should you need to store anything else that is not provided I recommend to keep it in the authentication ticket cookie that you create. And not in a session variable. Look at the sample code from the Microsoft article link I provided earlier. You can specify custom data inside the cookie.

     tkt = New FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now(), _
dateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data")
      cookiestr = FormsAuthentication.Encrypt(tkt)
      ck = new HttpCookie(FormsAuthentication.FormsCookieName(), cookiestr)
      if (chkPersistCookie.Checked) then ck.Expires=tkt.Expiration 
      ck.Path = FormsAuthentication.FormsCookiePath() 

Open in new window

jazjefAuthor Commented:
Thanks Yiogi for teaching me about the advanced authentication I need to learn/pursue.

Thanks Richard for the 4guysfromrolla resource; very helpful.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.