Advertisement

06.27.2008 at 01:41PM PDT, ID: 23522966
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Problem with login validation and stored procedure

Asked by woodje in Programming for ASP.NET

Tags:

Hello all,
I am writing a login form for my application I have got it to work with a simple stored procedure call to check if the user id and pwd exsist. Now I am trying to enchance the stored procedure to return information so that it can be used in either a cookie, session variable, or application variable. The problem I am running into is that when I enter a correct userid and pwd when I run the stored procedure on the sql server I get the return value of 1 for both stored procedures. Which is what I would expect. The problem is my asp application is not reading it right and is saying login failed.  Can you please take a look at my code below and let me know if you see something I am missing.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
--- working stored procedure (sp_loginvalidator)----
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
 
 
 
ALTER PROCEDURE [dbo].[sp_loginvalidator]
(@UserID nVarChar(50),
@Password nVarChar(50))
AS
RETURN
( 
SELECT Count(*)
FROM
Dbo.tblsecurity
WHERE UserID = @UserID AND Password = @Password
)
 
**** Return Value = 1 0r 0 ****
 
---- New stored procedure that works in sql but not in asp.net (security_validation)----
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
ALTER procedure [dbo].[security_validation] (
@userid nvarchar(25),
@password nvarchar(50),
@ruserid nvarchar(25) output,
@raccessid decimal(10, 0) output,
@remail nvarchar(65)output
) as
DECLARE @return_value int
select @userid = UserID, @password = Password, @ruserid = @userid, @raccessid = AccessID, @remail = email
From dbo.tblSecurity
Where UserID = @userid and Password = @password
if @@rowcount > 0
	Begin
	Select @return_value = 1
	return @return_value	
	end
else
	begin
	select @return_value = 0
	return @return_value
	end
 
**** this returns @ruserid = userid, @raccessid = accessid, @remail = email & Return Value = 1 or 0 ****
 
---- behind the scene code in Visual Basics ----
Sub btnLogin_OnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        If Page.IsValid Then    '   ||||| Meaning the Control Validation was successful!
            '   |||||   Connect to Database for User Validation |||||
            If DBConnection(txtuserid.Text.Trim(), txtpassword.Text.Trim()) Then
                Session("Logged_IN") = "Yes"    '   |||||   Use to Validate on other pages in the application                
 
                Dim aCookie As New HttpCookie("fud_info")
                aCookie.Values("userid") = txtuserid.Text
                'aCookie.Values("userid") = "@ruserid"
                'aCookie.Values("accessid") = "@raccessid"
                'aCookie.Values("email") = "@remail"
                'aCookie.Values("lastVisit") = DateTime.Now.ToString()
                aCookie.Expires = DateTime.Now.AddHours(4)
                Response.Cookies.Add(aCookie)
                Response.Redirect("default.aspx")
            Else
                Response.Redirect("failed.aspx")
 
                '   |||||   Credentials are Invalid
                '   |||||   Increment the LoginCount (attempts)
                'Session("LoginCount") = CInt(Session("LoginCount")) + 1
                ' ||||| Determine the Number of Tries
                'If Session("LoginCount").Equals(intMaxLoginAttempts) Then
                '	Response.Redirect("Denied.aspx")
                'End If
 
                'If CInt(Session("Num_of_Tries")) > 2 Then ' ||||| If Exceeds then Deny!
                '	Response.Redirect("Denied.aspx")
                'End If
 
            End If
        End If
    End Sub
 
Function DBConnection(ByVal struserid As String, ByVal strpassword As String) As Boolean
        '<sumamry>
        '   |||||   Declare Required Variables
        ' ||||| Access appSettings of Web.Config for Connection String (Constant)
        '</summary>
        '   |||||   This is the Connections Object for an SQL DB
        Dim MyConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("fss_caoConnectionString").ConnectionString)
 
        '<sumamry>
        '   |||||   Create a OleDb Command Object
        '   |||||   Pass in Stored procedure
        '   |||||   Set CommandType to Stored Procedure
        '</summary>
 
        ' ||||| To Access a Stored Procedure in SQL Server - Requires a Command Object
        Dim MyCmd As New SqlCommand("sp_loginvalidator", MyConn) ' |||| this is the line I change the stored procedure name. This name is the one that works.
 
        MyCmd.CommandType = CommandType.StoredProcedure
        '   |||||   Create Parameter Objects for values passed in
        Dim objParam1, objParam2 As SqlParameter
        '   |||||   Create a parameter to store your Return Value from the Stored Procedure
        Dim objReturnParam As SqlParameter
        'Dim objReturnParam1 As SqlParameter
        'Dim objReturnParam2 As SqlParameter
        'Dim objReturnparam3 As SqlParameter
        '<sumamry>
        '   |||||   Add the parameters to the parameters collection of the
        ' ||||| command object, and set their datatypes (OleDbType in this case)
        '</summary> 
        objParam1 = MyCmd.Parameters.Add("@userid", SqlDbType.NVarChar)
        objParam2 = MyCmd.Parameters.Add("@password", SqlDbType.NVarChar)
        objReturnParam = MyCmd.Parameters.Add("@return_value", SqlDbType.Int)
        'objReturnParam1 = MyCmd.Parameters.Add("@ruserid", SqlDbType.NVarChar)
        'objReturnParam2 = MyCmd.Parameters.Add("@raccessid", SqlDbType.NVarChar)
        'objReturnparam3 = MyCmd.Parameters.Add("@remail", SqlDbType.NVarChar)
 
        '   |||||   Set the direction of the parameters...input, output, etc
        objParam1.Direction = ParameterDirection.Input
        objParam2.Direction = ParameterDirection.Input
        objReturnParam.Direction = ParameterDirection.ReturnValue   '   Note RETURNVALUE
        'objReturnParam1.Direction = ParameterDirection.ReturnValue   '   Note RETURNVALUE
        'objReturnParam2.Direction = ParameterDirection.ReturnValue   '   Note RETURNVALUE
        'objReturnparam3.Direction = ParameterDirection.ReturnValue   '   Note RETURNVALUE
        '' ||||| Set the value(s) of the parameters to the respective source controls
        objParam1.Value = txtuserid.Text
        objParam2.Value = txtpassword.Text
 
        '   |||||   Try, catch block!
        Try
            '   |||||   Check if Connection to DB is already open, if not, then open a connection
            If MyConn.State = ConnectionState.Closed Then
                '   |||||   DB not already Open...so open it
                MyConn.Open()
                MyCmd.ExecuteNonQuery()
            End If
            '   |||||   Was the return value greater than 0 ???
            If objReturnParam.Value = 0 Then
                Return False
            Else
                Return True
            End If
 
            '   |||||   Close the Connection Closes with it
            MyConn.Close()
 
 
        Catch ex As Exception
            lblMessage2.Text = "Error Connecting to Database!"
        End Try
 
 
    End Function
 
Loading Advertisement...
 
[+][-]06.27.2008 at 10:56PM PDT, ID: 21889491

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Programming for ASP.NET
Tags: VB and ASP.NET 2.0
Sign Up Now!
Solution Provided By: anuragal
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.27.2008 at 10:59PM PDT, ID: 21889510

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.27.2008 at 11:02PM PDT, ID: 21889525

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628