ASP.NET MVC 4 - Custom Membership & Role Entities Not Updating

Chris McDonald

I am developing my first ASP.NET MVC 4 project, using C# and Entity Framework and I wish to implement Custom Membership & Roles, using my own SQL Server database and tables.

I have developed two classes, which are called 'CustomMembershipProvider' (inherits MembershipProvider) and 'CustomRoleProvider' (inherits RoleProvider). Both classes seem to work correctly on their first execution but when I manually update fields within SQL Server (e.g. change the Right Name or Right Description), the LINQ query I perform on the Entity appears to be out of date.

For clarity, Roles are controlled within my Rights table (tblRights), which contains field names like 'RightName' and 'RightDescription'.

Below is a sample of the code I execute, which returns a string array containing Role/Right values like "Operator" and "Manager":

namespace MyProject.Classes
{
    public class CustomRoleProvider : RoleProvider
    {
        private MyEntities db = new MyEntities();

        public override string[] GetRolesForUser(string username)
        {

            tblOperator _tblOperator = (from prod in db.tblOperators
                                        where prod.Username == username
                                        select prod).FirstOrDefault();

            var roles = _tblOperator.tblOperatorRights.Where(m => m.WorkCentreID == 1).ToArray();

            return roles.Select(m => m.tblRight.RightName).ToArray();
        }
    }
}

Now assuming I manually update SQL Server and change the Role name from "Manager" to "Administrator", the next time this code executes, it will still return "Operator" and "Manager", not "Operator" and "Administrator".

Can anyone see where I am going wrong? You assistance is much appreciated.

Regards, Chris

Chris McDonald

Thanks to @BenRobinson,

So simple, yet so effective! I am new to C#, so little things like this keep evading me! Thank you! The completed code is below:

namespace MyProject.Classes
{
    public class CustomRoleProvider : RoleProvider
    {
        private MyEntities db;

        public override string[] GetRolesForUser(string username)
        {
            db = new MyEntities();

            tblOperator _tblOperator = (from prod in db.tblOperators
                                        where prod.Username == username
                                        select prod).FirstOrDefault();

            var roles = _tblOperator.tblOperatorRights.Where(m => m.WorkCentreID == 1).ToArray();

            return roles.Select(m => m.tblRight.RightName).ToArray();
        }
    }
}

Regards, Chris

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add role to user in Simple Membership ASP NET MVC 4

ASP.NET MVC 4 error updating entity framework models with related entities

How to disable Membership Providers completely in ASP.NET MVC 4

Can we use Asp.Net Membership and Role Provider in MVC 5

Custom Role Provider with ASP.net MVC -- Changing Roles

ASP.NET MVC custom role provider not working

ASP.NET MVC How to create a custom role provider

Custom role provider in ASP MVC

.NET Membership in ASP.NET MVC4 and Entity Framework with Oracle as Db

Custom redirection in ASP.NET MVC 4

ASP.NET MVC 4 Custom URL

ASP.net 4.5 Membership and Roles add user to role

How to Edit in Membership Asp.net MVC 4 to make another table with relationship?

updating object using NHibernate in Asp.net MVC4

updating portion of asp.net MVc 4 view with partial view

add membership subscription to asp.net mvc web application

ASP.NET MVC 5 Membership impersonate specific user

Strange behavior of Membership.GetUser() in ASP.NET MVC

ASP.NET Core 3.1 MVC Access Denied role based authorization - Conflict with custom UserClaimsPrincipalFactory

ASP.NET MVC 4 Custom 404 Page

Custom URL Routing in Asp.Net MVC 4

ASP.NET MVC 4 Custom Handle Error

Custom validation summary in ASP.NET MVC 4

Adding a custom class to Radio Buttons in ASP.NET MVC 4

ASP.Net MVC 4 Custom ValidationAttribute Dependency Injection

Client side validation for custom validator asp.net MVC 4

Creating my custom security role and custom user group tables, to implement custom authorization for my asp.net mvc web application

Change role permissions in ASP .NET MVC

asp.net identity with custom role