Juventino
Juventino

Reputation: 21

How to call aspnet_UsersInRoles_IsUserInRole Stored Procedure with Dapper ORM

I need to call aspnet_UsersInRoles_IsUserInRole from Aspnet Membership.Im making dapper call like this:

public int CheckIfUserIsInRole(IsUserInRole userInRole)
    {
        using (var connection = new SqlConnection(ConfigurationSettings.GetConnectionString()))
        {
            DynamicParameters param = new DynamicParameters();
            param.Add("@UserName", userInRole.UserName);
            param.Add("@ApplicationName", userInRole.ApplicationName);
            param.Add("@RoleName", userInRole.RoleName);

         return    connection.Query("aspnet_UsersInRoles_IsUserInRole", param, commandType: CommandType.StoredProcedure).FirstOrDefault();               
        }
    }

And in controller i add:

    public int IsUserInRole(IsUserInRole isUserInRole)
    {            
        var model = _userRepository.CheckIfUserIsInRole(new IsUserInRole()
        {
            UserName = "testuser",
            RoleName = "user",
            ApplicationName = "USERMANAGEMENT"
        });

        return model;
    }

The user exist and have the correct role but every time returns 0. Here is the Stored Procedure from AspNet Membership:

ALTER PROCEDURE [dbo].[aspnet_UsersInRoles_IsUserInRole]
@ApplicationName  nvarchar(256),
@UserName         nvarchar(256),
@RoleName         nvarchar(256)

AS BEGIN DECLARE @ApplicationId uniqueidentifier SELECT @ApplicationId = NULL SELECT @ApplicationId = ApplicationId FROM aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName IF (@ApplicationId IS NULL) RETURN(2) DECLARE @UserId uniqueidentifier SELECT @UserId = NULL DECLARE @RoleId uniqueidentifier SELECT @RoleId = NULL

SELECT  @UserId = UserId
FROM    dbo.aspnet_Users
WHERE   LoweredUserName = LOWER(@UserName) AND ApplicationId = @ApplicationId

IF (@UserId IS NULL)
    RETURN(2)

SELECT  @RoleId = RoleId
FROM    dbo.aspnet_Roles
WHERE   LoweredRoleName = LOWER(@RoleName) AND ApplicationId = @ApplicationId

IF (@RoleId IS NULL)
    RETURN(3)

IF (EXISTS( SELECT * FROM dbo.aspnet_UsersInRoles WHERE  UserId = @UserId AND RoleId = @RoleId))
    RETURN(1)
ELSE
    RETURN(0)

END

Where I'm mistaking? Any advice how to fix it ?

I need this Stored Procedure to check if the user is in that role so i can use it for [AuthorizeRoles("RoleTest")]

Upvotes: 0

Views: 284

Answers (1)

Richard Deeming
Richard Deeming

Reputation: 31208

That stored procedure doesn't return any records; it uses the return value instead. This needs to be handled as a parameter:

public int CheckIfUserIsInRole(IsUserInRole userInRole)
{
    using (var connection = new SqlConnection(ConfigurationSettings.GetConnectionString()))
    {
        DynamicParameters param = new DynamicParameters();
        param.Add("@UserName", userInRole.UserName);
        param.Add("@ApplicationName", userInRole.ApplicationName);
        param.Add("@RoleName", userInRole.RoleName);
        param.Add("@ReturnValue", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

        connection.Execute("aspnet_UsersInRoles_IsUserInRole", param, commandType: CommandType.StoredProcedure);

        return param.Get<int>("@ReturnValue");
    }
}

https://github.com/StackExchange/dapper-dot-net#stored-procedures

(Also posted to your copy of this question on CodeProject.)

Upvotes: 0

Related Questions