Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with custom role provider

Status
Not open for further replies.

BFarley

Programmer
May 22, 2008
43
US
I created a custom role provider for my application, but it doesn't return the proper value for IsUserInRole.

Section of web.config that defines the provider:
Code:
		<roleManager defaultProvider="MyTestAppRoleProvider" enabled="true">
			<providers>
				<clear/>
				<add name="MyTestAppRoleProvider" connectionStringName="MyTestAppDb" applicationName="MyTestApp" type="MyTestAppRoleProvider"/>
			</providers>
		</roleManager>

Provider code:
Code:
Imports System.Data
Imports System.Data.SqlClient

Public Class MyTestAppRoleProvider
	Inherits SqlRoleProvider

	Private _strConnection As String

	Public Sub New()
		_strConnection = ConfigurationManager.ConnectionStrings("MyTestAppDb").ConnectionString
	End Sub

	Public Overrides Function IsUserInRole(ByVal strUsername As String, ByVal strRoleName As String) As Boolean


		Using sqlConn As New SqlConnection(_strConnection)
			sqlConn.Open()

			Using sqlCmd As New SqlCommand

				Dim parApplicationName As New SqlParameter
				With parApplicationName
					.ParameterName = "@strApplicationName"
					.DbType = DbType.String
					.Size = 256
					.Direction = ParameterDirection.Input
					.Value = "MyTestApp"
				End With

				Dim parUsername As New SqlParameter
				With parUsername
					.ParameterName = "@strUsername"
					.DbType = DbType.String
					.Size = 256
					.Direction = ParameterDirection.Input
					.Value = strUsername
				End With

				Dim parRoleName As New SqlParameter
				With parRoleName
					.ParameterName = "@strRoleName"
					.DbType = DbType.String
					.Size = 256
					.Direction = ParameterDirection.Input
					.Value = strRoleName
				End With

				Dim parReturnValue As New SqlParameter
				With parReturnValue
					.DbType = DbType.Int32
					.Direction = ParameterDirection.ReturnValue
				End With

				With sqlCmd
					.Connection = sqlConn
					.CommandText = "dbo.MyTestApp_UsersInRoles_IsUserInRole"
					.CommandType = CommandType.StoredProcedure
					.Parameters.Add(parApplicationName)
					.Parameters.Add(parUsername)
					.Parameters.Add(parRoleName)
					.Parameters.Add(parReturnValue)
					.ExecuteNonQuery()
				End With

				Return parReturnValue.Value = 1
			End Using
		End Using
	End Function

End Class

Where I'm using it in a form, and checking whether user is in the Administrator role:
Code:
Label3.Text = Roles.IsUserInRole(User.Identity.Name, "Administrator")

The stored procedure, when run by itself, returns "1", so that part appears to be correct.

I've also tested the connection string, and it's working when called from a page.

The user is able to log in with the use of a custom membership provider.

Is it a problem that I'm inheriting from SqlRoleProvider instead of RoleProvider?

I'm sure there's something small I'm overlooking.

Any pointers?

Thanks in advance!

Bryant Farley

"The Dude Abides
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top