using session state dont work properly - asp.net

i am still new to using session state, i want to convert page name into and integer according to a database table
a function then compares "X" and "Y" to check if a user have the right to view this page
i know this is not the best way of managing website security, but it is like "training on how to use the session"
what have i done wrong
Partial Class advancedsearch
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Label1.Text = Session("username").ToString
Label3.Text = Session("role").ToString
Label4.Text = System.IO.Path.GetFileName(Request.Url.ToString())
Catch ex As Exception
Response.Redirect("login.aspx")
End Try
If Label1.Text = "" Then
Response.Redirect("login.aspx")
End If
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("default.aspx")
End Sub
End Class

try putting the comparison part in pre render complete
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub

Related

Asp.net Vb.net , access control concurrency

Hello guys I am trying to make restriction on my WebApp that runs on VB.net with framework Asp.net. In another words, how can I Control the permitted number of sessions.
The WebApp works perfectly and now I want to add "How many users can access the WebApp" if Number of users "Sessions" exceeded the new coming users will be redirected to a site.
I have been working on this for few weeks... tried Cookies and Seasons and neither is working
Scenario: Maximum users can access the WebApp is 1
when I launch the second Browser and start new session the UserNumber will be incremented by 1 hence will be redirected to "ServerBusy" page...
However, If the second Browser opened new tab and accessed the WebApp that will bypass the Condition at Session_Start and will access the WebApp .
Thank you.
here is the Code
Imports System.Web
Public Class Global_asax
Inherits System.Web.HttpApplication
Public Shared maxUsersAllowance As Integer = 1 'Total number of users can enter the server
Public Shared UserNumber As Integer = 0 ' the current number of users
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
UserNumber = UserNumber + 1
Dim myCookie As New HttpCookie("UserNumber")
Dim myCookie2 As New HttpCookie("Access")
myCookie.Value = UserNumber
myCookie2.Value = True
Response.Cookies.Add(myCookie)
Response.Cookies.Add(myCookie2)
If (Request.Cookies("UserNumber") IsNot Nothing) Then
If (Request.Cookies("UserNumber").Value > 1) Then
If (Request.Cookies("Access").Value) Then
Response.Redirect("~/ServerBusy.aspx")
End If
End If
End If
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Init(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Dispose(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Unload(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
After Reading Article https://stackoverflow.com/a/6218525/1260204 and Updating the code, the Issue still exist which is that the User Can get into the WebApp if he open new Tab on the browser and connected
Imports System.Web
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
Application("ActiveSessions") = 0
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Try
Application.Lock()
Dim activeSessions As Integer = CInt(Application("ActiveSessions")) + 1
Dim allowedSessions As Integer = 1
' retrieve the threshold here instead
Application("ActiveSessions") = activeSessions
If activeSessions > allowedSessions Then
System.Web.HttpContext.Current.Response.Redirect("~/ServerBusy.aspx", False)
End If
Finally
Application.UnLock()
End Try
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim LiveSessionsCount As Integer = CInt(Application("LiveSessionsCount"))
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("ActiveSessions") = CInt(Application("ActiveSessions")) - 1
Application.UnLock()
End Sub
End Class
Update, I have solved the Issue with the Tabs. by adding
Session.Abandon()
In my redirected page. However that is half solution as far as i can get to... the other half is , if the user closes his browser by the X , I do need to wait 20 minutes until the session ends ... is there a away to terminate the session once the user exit/kill the page?

asp.net (with vb.net) control array at run time giving error : Object reference not set to an instance of an object

asp.net (with vb.net) control array at run time giving error :
Object reference not set to an instance of an object.
I am getting this error.
My Code is :
Partial Class _Default
Inherits System.Web.UI.Page
Dim chk(10) As CheckBox
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
MsgBox("loading")
chk(0) = New CheckBox
With chk(0)
.ID = "chk(0)"
.Text = .ID
End With
Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If chk(0).Checked = True Then
MsgBox("Yes")
Else
MsgBox("No")
End If
Response.Redirect("Page1.aspx")
End Sub
End Class
Use this instead:
Dim chk As CheckBox
chk = New CheckBox
Solved !
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Else
MsgBox("loading")
chk(0) = New CheckBox
With chk(0)
.ID = "chk(0)"
.Text = .ID
End With
Me.form1.Controls.Add(chk(0))
End If
TextBox1.Text = chk(0).Text
End Sub

why is recaptcha.IsValid always returns True

why is recaptcha.IsValid always returns True?
Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles CreateUserWizard1.CreatingUser
Dim Captcha As RecaptchaControl = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("recaptcha1"), RecaptchaControl)
If Not Captcha.IsValid Then
e.Cancel = True
End If
End Sub
call Captcha.Validate() before Captcha.IsValid
Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles CreateUserWizard1.CreatingUser
Dim Captcha As RecaptchaControl = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("recaptcha1"), RecaptchaControl)
Captcha.Validate()
If Not Captcha.IsValid Then
e.Cancel = True
End If
End Sub

form_load issue with updating details using session variables and textboxes

I'm trying to allow users to update their details after logging in on an asp site using vb. Textboxes are populated with user details using session variables in the form_load. The textboxes should be editable but for some reason are not registering the changes when the submit button is clicked.
There is a similar question with the same issue Database not updating after UPDATE SQL statement in ASP.net that was never answered.
Please can someone advise
Thanks in advance
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Txt_Fname.Text = Session("First_Name")
Txt_LName.Text = Session("Last_Name")
Txt_ContactNumber.Text = Session("Cell_Number")
Txt_Email.Text = Session("Email_Address")
End Sub
Protected Sub Cmd_Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cmd_Submit.Click
Command.Connection = Connection
Command.CommandText = "UPDATE dbo.User_Account Set First_Name = #First_Name, Last_Name = #Last_Name, Cell_Number = #Cell_Number, Email_Address = #Email_Address where Overall_ID = #Overall_ID"
Command.Parameters.AddWithValue("#First_Name", Txt_Fname.Text)
Command.Parameters.AddWithValue("#Last_Name", Txt_LName.Text)
Command.Parameters.AddWithValue("#Cell_Number", Txt_ContactNumber.Text)
Command.Parameters.AddWithValue("#Email_Address", Txt_Email.Text)
Command.Parameters.AddWithValue("#Overall_ID", Session("ID"))
Connection.Open()
Command.ExecuteNonQuery()
Connection.Close()
Response.Redirect("MyAccount.aspx")
End Sub
Add if not page.ispostback before your code in page_load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) then
Txt_Fname.Text = Session("First_Name")
Txt_LName.Text = Session("Last_Name")
Txt_ContactNumber.Text = Session("Cell_Number")
Txt_Email.Text = Session("Email_Address")
End If
End Sub

Unique number for unique visitor on button click in vb.net?

How generate the unique no. 1,2,3 and so on .... on button click of each new user ..
the code mentioned below is a readwrite coding in vb.net ...
but the problem is it generate the same id for different users on button click event... but i want the no. of times button clicked the new ids will be generated
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILE_NAME As String = Server.MapPath("counts.vbi")
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(Literal1.Text)
objWriter.Close()
Else
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim FILE_NAME As String = Server.MapPath("counts.vbi")
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objStreamReader As New System.IO.StreamReader(FILE_NAME)
Literal1.Text = objStreamReader.ReadToEnd
Literal1.Text += 1
objStreamReader.Close()
End If
End Sub
You can generate Random Id like this :
Guid.NewGuid().ToString("N")
Hope This can Help You.
Thanks

Resources