0

I would like to change GetRolesForUser in the ASP .net MVC authorisation string to int. I would like to get value by user type id.

It was because I have multiple tables. So I would like to retrieve value by user type id. Is there any way to change to int for authentication?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using mylatestproject.Models;

namespace mylatestproject.MyRoleProvider
{
    public class SiteRole : RoleProvider
    {
        public override string ApplicationName { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        { throw new NotImplementedException(); }

      public override string[] GetRolesForUser(string username)
        {
            attendanceselangorEntities db = new attendanceselangorEntities();
            string data = db.tblusers.Where(x => x.userNm == username).FirstOrDefault().userNm;
            string[] result = { data };
            return result;
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

This source code has no error, But I want to change it to int.

Kampai
  • 21,517
  • 19
  • 87
  • 90
Nur Afiq
  • 1
  • 2

1 Answers1

0

There are two primary reasons for creating a custom role provider:

  • You need to store role information in a data source that is not supported by the role providers included with the .NET Framework, such as a FoxPro database, an Oracle database, or other data sources.

  • You need to manage role information using a database schema that is different from the database schema used by the providers that ship with the .NET Framework. A common example of this would be role data that already exists in a SQL Server database for a company or Web site.

In your case, it seems a custom Authorize filter can form a better solution. some good threads:

If using a custom role provider is a must, change your viewpoint and pass userID as string and manage the logic inside GetRolesForUser to convert ID back to int and return the associate user Roles from your multiple tables.


Please comment for more clarification.

A. Nadjar
  • 1,909
  • 2
  • 15
  • 18