Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "Select count (*) from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
Dim cmdStr2 As String = "Select Password from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim pass As New SqlCommand(cmdStr2, DatabaseW7ConnectionString)
Dim password As String = pass.ExecuteScalar().ToString()
DatabaseW7ConnectionString.Close()
<div>I dont know how to continue from here to login and redirect to a specific page based on login authorization..Really need your advice. Thank you</div> <div></div>
First of all you should be creating a procedure in your sql database as below to check the user authentication:
Create Procedure CheckUserDetails
(@userName varchar(40),@password varchar(40)
)
as
Begin
select count(*) from <yourUserName_Password_table> where userName=@userName and password=@password End
the above procedure will take two argument userName and password that you will be passing from your aspx code. now add one more text box for password(name it TextBoxPassword) as you've already added textbox for user name. Now use the code below on your button login:
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim userName as String
Dim password as Password
userName=TextBoxUserName.Text ' you need to ask the user name from user
password=TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType=CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@userName",SqlDbType.Varchar,40))
Checkuser.Parameters.Add(New SqlParameter("@password",SqlDbType.Varchar,40))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result as String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("YourNextPage.aspx")
PS: I dont have very good command on VB syntax, so you need to fix the typo :)
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
1. And what I need to write if user login with wrong username or password?How to make it got notice "Your username or password entered is invalid"
2. I got problem said "CommandType and SqlDbType" is not declared. It may be inaccessible due to its protection level". I don't know how to fix it. Cn you fix it?
For the problem to notify user if password is wrong you can show him like below:
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim userName as String
Dim password as Password
userName=TextBoxUserName.Text ' you need to ask the user name from user
password=TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType=CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@userName",SqlDbType.Varchar,40))
Checkuser.Parameters.Add(New SqlParameter("@password",SqlDbType.Varchar,40))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result as String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("YourNextPage.aspx")
Else
Response.Write("<script>alert('User name or password is incorrect');</script>");
EndIf
can you post your complete code, we'll look into it and then find the solution for your queries
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim UserName As String
Dim Password As String
userName = TextBoxUserName.Text ' you need to ask the user name from user
password = TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType = CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.nVarchar, 40))
Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.Varchar, 40))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result As String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("~/Admin/HomeAdmin.aspx")
End If
End Sub
End Class
#I have follow the step that you give including insert into stored procedures and below is code for login.aspx
<%@ Page Title="" Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 145px;
}
.style3
{
width: 128px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<p>
</p>
<p>
<br />
<table class="style1">
<tr>
<td class="style2">
Username</td>
<td class="style3">
<asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUserName" runat="server"
ControlToValidate="TextBoxUserName" ErrorMessage="UserName is Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
Password</td>
<td class="style3">
<asp:TextBox ID="TextBoxPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorPassword" runat="server"
ControlToValidate="TextBoxPassword" ErrorMessage="Password is Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="ButtonLogin" runat="server" Height="46px" Text="Login"
Width="84px" />
</td>
<td>
</td>
</tr>
</table>
</p>
</asp:Content>
your aspx markup is fine, just change code below to your code and let me know:
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim UserName As String
Dim Password As String
userName = TextBoxUserName.Text ' you need to ask the user name from user
password = TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType = CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.nVarchar, 40))
Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.Varchar, 40))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result As String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("~/Admin/HomeAdmin.aspx")
Else
Response.Write("<script>alert('Invalid user name or password, please check the credentials');</script>")
Return
End If
End Sub
End Class
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
1. I don't know why this happen. when I checked again at stored procedures, the command became like this:
ALTER PROCEDURE CheckUserDetails
(@UserName nvarchar(24),@Password nvarchar(24)
)
as
Begin
select count(*) from Pat where UserName=@UserName and
Password=@Password End
2. I got this message after I enter the login button.
Server Error in '/WebSite7' Application.
Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Source Error:
Line 19: Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.NVarChar, 24))
Line 20: Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.NVarChar, 24))
Line 21: Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) Line 22: If temp = 1 Then
Line 23: DatabaseW7ConnectionString.Close()
PS - Why you want to re-invent the wheel when you have Forms Authentication along with Roles Manager in ASP.NEt by default...i advise you to rethink to use them...
What I understand is you want me to use the default database(ASPNETDB.mdf) contains all database functions like (aspnet_Applications, aspnet_Membership and so on) that have been provided and expand it only. Is it what you mean?
hanis
Member
44 Points
35 Posts
how to create custom login
Mar 23, 2012 03:12 AM|LINK
hye..
This is my code.
Imports System.Data.SqlClient
<div>I dont know how to continue from here to login and redirect to a specific page based on login authorization..Really need your advice. Thank you</div> <div></div>Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "Select count (*) from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
Dim cmdStr2 As String = "Select Password from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim pass As New SqlCommand(cmdStr2, DatabaseW7ConnectionString)
Dim password As String = pass.ExecuteScalar().ToString()
DatabaseW7ConnectionString.Close()
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: how to create custom login
Mar 23, 2012 04:23 AM|LINK
First of all you should be creating a procedure in your sql database as below to check the user authentication:
the above procedure will take two argument userName and password that you will be passing from your aspx code. now add one more text box for password(name it TextBoxPassword) as you've already added textbox for user name. Now use the code below on your button login:
Imports System.Data.SqlClient Partial Class Login Inherits System.Web.UI.Page Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString) Dim userName as String Dim password as Password userName=TextBoxUserName.Text ' you need to ask the user name from user password=TextBoxPassword.Text ' you need to ask the password from user DatabaseW7ConnectionString.Open() Dim cmdStr As String = "CheckUserDetails" Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString) Checkuser.CommandType=CommandType.StoredProcedure Checkuser.Parameters.Add(New SqlParameter("@userName",SqlDbType.Varchar,40)) Checkuser.Parameters.Add(New SqlParameter("@password",SqlDbType.Varchar,40)) Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) If temp = 1 Then DatabaseW7ConnectionString.Close() Dim result as String = "User is Authenticated, Now you can Redirect him" Response.Redirect("YourNextPage.aspx")PS: I dont have very good command on VB syntax, so you need to fix the typo :)
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
bhushan_bhar...
Participant
1498 Points
581 Posts
Re: how to create custom login
Mar 23, 2012 06:29 AM|LINK
See this link sir....
http://usingaspdotnet.blogspot.in/2011/03/form-authenticationbig-security-in.html
Useofasp.net/
Howtouseasp.net
hanis
Member
44 Points
35 Posts
Re: how to create custom login
Mar 23, 2012 09:16 AM|LINK
1. And what I need to write if user login with wrong username or password?How to make it got notice "Your username or password entered is invalid"
2. I got problem said "CommandType and SqlDbType" is not declared. It may be inaccessible due to its protection level". I don't know how to fix it. Cn you fix it?
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: how to create custom login
Mar 23, 2012 09:59 AM|LINK
For the problem to notify user if password is wrong you can show him like below:
Imports System.Data.SqlClient Partial Class Login Inherits System.Web.UI.Page Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString) Dim userName as String Dim password as Password userName=TextBoxUserName.Text ' you need to ask the user name from user password=TextBoxPassword.Text ' you need to ask the password from user DatabaseW7ConnectionString.Open() Dim cmdStr As String = "CheckUserDetails" Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString) Checkuser.CommandType=CommandType.StoredProcedure Checkuser.Parameters.Add(New SqlParameter("@userName",SqlDbType.Varchar,40)) Checkuser.Parameters.Add(New SqlParameter("@password",SqlDbType.Varchar,40)) Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) If temp = 1 Then DatabaseW7ConnectionString.Close() Dim result as String = "User is Authenticated, Now you can Redirect him" Response.Redirect("YourNextPage.aspx") Else Response.Write("<script>alert('User name or password is incorrect');</script>"); EndIfcan you post your complete code, we'll look into it and then find the solution for your queries
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
hanis
Member
44 Points
35 Posts
Re: how to create custom login
Mar 23, 2012 10:06 AM|LINK
Imports System.Data.SqlClient Partial Class Login Inherits System.Web.UI.Page Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString) Dim UserName As String Dim Password As String userName = TextBoxUserName.Text ' you need to ask the user name from user password = TextBoxPassword.Text ' you need to ask the password from user DatabaseW7ConnectionString.Open() Dim cmdStr As String = "CheckUserDetails" Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString) Checkuser.CommandType = CommandType.StoredProcedure Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.nVarchar, 40)) Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.Varchar, 40)) Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) If temp = 1 Then DatabaseW7ConnectionString.Close() Dim result As String = "User is Authenticated, Now you can Redirect him" Response.Redirect("~/Admin/HomeAdmin.aspx") End If End Sub End Class #I have follow the step that you give including insert into stored procedures and below is code for login.aspx <%@ Page Title="" Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <style type="text/css"> .style1 { width: 100%; } .style2 { width: 145px; } .style3 { width: 128px; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> <p> </p> <p> <br /> <table class="style1"> <tr> <td class="style2"> Username</td> <td class="style3"> <asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidatorUserName" runat="server" ControlToValidate="TextBoxUserName" ErrorMessage="UserName is Required"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> Password</td> <td class="style3"> <asp:TextBox ID="TextBoxPassword" runat="server" TextMode="Password"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidatorPassword" runat="server" ControlToValidate="TextBoxPassword" ErrorMessage="Password is Required"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> </td> <td class="style3"> </td> <td> </td> </tr> <tr> <td class="style2"> </td> <td class="style3"> <asp:Button ID="ButtonLogin" runat="server" Height="46px" Text="Login" Width="84px" /> </td> <td> </td> </tr> </table> </p> </asp:Content>Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: how to create custom login
Mar 23, 2012 10:24 AM|LINK
your aspx markup is fine, just change code below to your code and let me know:
Imports System.Data Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString) Dim UserName As String Dim Password As String userName = TextBoxUserName.Text ' you need to ask the user name from user password = TextBoxPassword.Text ' you need to ask the password from user DatabaseW7ConnectionString.Open() Dim cmdStr As String = "CheckUserDetails" Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString) Checkuser.CommandType = CommandType.StoredProcedure Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.nVarchar, 40)) Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.Varchar, 40)) Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) If temp = 1 Then DatabaseW7ConnectionString.Close() Dim result As String = "User is Authenticated, Now you can Redirect him" Response.Redirect("~/Admin/HomeAdmin.aspx") Else Response.Write("<script>alert('Invalid user name or password, please check the credentials');</script>") Return End If End Sub End ClassBlog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
hanis
Member
44 Points
35 Posts
Re: how to create custom login
Mar 23, 2012 10:40 AM|LINK
1. I don't know why this happen. when I checked again at stored procedures, the command became like this:
ALTER PROCEDURE CheckUserDetails
(@UserName nvarchar(24),@Password nvarchar(24)
)
as
Begin
select count(*) from Pat where UserName=@UserName and
Password=@Password End
2. I got this message after I enter the login button.
Server Error in '/WebSite7' Application.
Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Source Error:
Line 19: Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.NVarChar, 24)) Line 20: Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.NVarChar, 24)) Line 21: Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) Line 22: If temp = 1 Then Line 23: DatabaseW7ConnectionString.Close()ramiramilu
All-Star
95403 Points
14096 Posts
Re: how to create custom login
Mar 23, 2012 10:53 AM|LINK
Custom Authentication tutorial with code - http://thefrozencoder.ca/post/2009/01/25/Rolling-Your-Own-Custom-Authentication-For-ASPNET.aspx
PS - Why you want to re-invent the wheel when you have Forms Authentication along with Roles Manager in ASP.NEt by default...i advise you to rethink to use them...
Thanks,
JumpStart
hanis
Member
44 Points
35 Posts
Re: how to create custom login
Mar 23, 2012 11:03 AM|LINK
What I understand is you want me to use the default database(ASPNETDB.mdf) contains all database functions like (aspnet_Applications, aspnet_Membership and so on) that have been provided and expand it only. Is it what you mean?