2 Replies - 250 Views - Last Post: 03 August 2019 - 04:53 AM Rate Topic: -----

#1 SteveHi   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 07-June 15

Using Session

Posted 02 August 2019 - 03:29 AM

Hello

I have a log-in page and when users log-in, they are taken to userpage.aspx. It allows them to upload files, and that is the only reason why any user needs to log in. From what I can see locally, the 'upload file' part of the following code works. After uploading his file, the user then clicks the log-out button, but what if he copies and pastes the URL of the username.aspx page to avoid the bother of logging-in in future?

I know that 'Session' is involved in the code and this is what I have in username.aspx.vb (there are no namespaces):

Public Class Userpage
    Inherits System.Web.UI.Page

    Private _message As String

    Private Property Message As String
        Get
            Return _message
        End Get
        Set(value As String)
            _message = value
        End Set
    End Property

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim Email As String = "Email".ToString()

            If Not String.IsNullOrEmpty(Request.QueryString("Email")) Then
                Email = Request.QueryString("Email")
            End If

            Label2.Text = "Hello " & Email & ", welcome!"

        End If

        If Not String.IsNullOrEmpty(Request.QueryString("Email")) Then

          Label1.Text = "Please upload your file"

            BtnLogout.Visible = True

           End If

         End Sub

  Protected Sub BtnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnUpload.Click

        If MyFileUpload.HasFile Then

            Try

                MyFileUpload.SaveAs("C:/Uploads/" & MyFileUpload.FileName)

                Label5.Visible = False

                Label5.Text = "File name: " &
        MyFileUpload.PostedFile.FileName & "<br>" &
        "File Size: " &
        MyFileUpload.PostedFile.ContentLength & "<br>" &
        "Content type: " &
        MyFileUpload.PostedFile.ContentType & "<br>" &
        "Location Saved: C:/Uploads/" & MyFileUpload.FileName

                Label5.Visible = True

            Catch ex As Exception

                If ex.Message.ToString().ToLower().Contains("could not find part of the path") Then

                    Label4.Text = "Error: Path unavailable"

                    Label2.Visible = False

                End If

            End Try
        Else
            Label3.Text = "You have not specified a file"

            Label2.Visible = False

        End If

    End Sub

Sub SaveFile(ByVal file As HttpPostedFile)

        Dim savePath As String = String.Empty
        Dim fileName As String = String.Empty
        Dim pathToCheck As String = savePath & fileName
        Dim tempfileName As String = String.Empty

        If (System.IO.File.Exists(pathToCheck)) Then
            Dim counter As Integer = 2
            While (System.IO.File.Exists(pathToCheck))
                ' If a file with this name already exists,
                ' prefix the filename with a number.
                tempfileName = counter.ToString() + fileName
                pathToCheck = savePath + tempfileName
                counter = counter + 1
            End While

            fileName = tempfileName

            ' Notify the user that the file name was changed.
            Label5.Text = "A file with the same name already exists." + "<br />" +
    "Your file was saved as " + fileName

            Else

           Label5.Text = "File name: " &
    MyFileUpload.PostedFile.FileName & "<br>" &
    "File Size: " &
    MyFileUpload.PostedFile.ContentLength & "<br>" &
    "Content type: " &
    MyFileUpload.PostedFile.ContentType & "<br>" &
    "Location Saved: C:/Uploads/" & MyFileUpload.FileName

          End If

        ' Append the name of the file to upload to the path.
        savePath += fileName

        ' Use SaveAs to save the uploaded
        ' file to the specified directory.
        MyFileUpload.SaveAs(savePath)

    End Sub

 Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click

        Session.RemoveAll()
        Session.Clear()
        Session.Abandon()
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1))
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Cache.SetNoStore()
        Response.Redirect("index.aspx")

    End Sub

End Class


Does the above code look viable, please? I have no online server at the moment, so I can't test it.

Many thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Using Session

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,216
  • Joined: 12-January 10

Re: Using Session

Posted 02 August 2019 - 07:18 AM

on login store the user id into the session

session("UserID) = where ever you want to get info from


on next page you can use the session("UserID") anywhere you want
on closing out you can set
session("UserID") = string.empty


do a test to see if the session has an user id and that avoids the copy paste crap
Was This Post Helpful? 0
  • +
  • -

#3 SteveHi   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 07-June 15

Re: Using Session

Posted 03 August 2019 - 04:53 AM

That sounds logical, DarenR, thanks.

I assume you mean that

session("UserID) = where ever you want to get info from


is to be used in my login.aspx.vb, and

session("UserID") = string.empty


maybe here in my userpage.aspx.vb:

Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click 

       [b]Session("UserID") = string.empty[/b]
        Session.RemoveAll() 
        Session.Clear() 
        Session.Abandon() 
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)) 
        Response.Cache.SetCacheability(HttpCacheability.NoCache) 
        Response.Cache.SetNoStore() 
        Response.Redirect("index.aspx") 
    
End Sub 



Many thanks again, you have been a great help.

Steve
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1