why is recaptcha.IsValid always returns True - asp.net

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

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

How to provide a common style for DataGridView in VB.net

How to create a class or some other stuff to make all the DataGridView inside my project of same format i:e AlternativeRowColor, ForColor,BackColor and Other properties. Currently i have to go to each of the control property to set , it sucks when user requested to change and property of Grid as i have to change in all the DataGridView.
Public Class FrmArticle
Private Sub GridFormatting(ByVal DGV As DataGridView)
DGV.ForeColor = Color.Black
DGV.BackgroundColor = Color.AliceBlue
DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown
DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue
DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue
DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod
DGV.EnableHeadersVisualStyles = False
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GridFormatting(DataGridView1)
End Sub
End Class
or
Module GridFormat
Public Sub GridFormatting(ByVal DGV As DataGridView)
DGV.ForeColor = Color.Black
DGV.BackgroundColor = Color.AliceBlue
DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Brown
DGV.AlternatingRowsDefaultCellStyle.ForeColor = Color.DodgerBlue
DGV.ColumnHeadersDefaultCellStyle.ForeColor = Color.CadetBlue
DGV.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGoldenrod
DGV.EnableHeadersVisualStyles = False
End Sub
End Module
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GridFormatting(DataGridView1)
End Sub

DropDownList1.SelectedIndexChanged not working

Protected Sub DropDownList1_SelectedIndexChanged(
ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Context.Response.Write("selectedindexchanged")
End Sub
Why is this not working?
Have you put Autopostback = true for the dropdown list

using session state dont work properly

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

Resources