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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Simple Membership ASP NET MVC4でユーザーに役割を追加する

分類Dev

asp.net identity with custom role

分類Dev

ASP.NET MVCでのMembership.GetUser()の奇妙な動作

分類Dev

ASP.NET MVC 5 Custom Scaffolding Option [t4 templates]

分類Dev

Custom Configuration Settings for Specific ASP.NET MVC4 URLs

分類Dev

Custom Authorize Attribute on asp.net mvc

分類Dev

User management with ASP.NET MVC 4

分類Dev

ASP.NET MVC4 ActionFilters

分類Dev

MVC 4ASP.NET構成

分類Dev

Hiding routes in ASP.Net MVC 4

分類Dev

asp.NET MVC entity validation depends on other entities of the same type

分類Dev

ASP.NET MVC custom multiple fields validation

分類Dev

Asp.net 5 MVC 6 Custom authentication

分類Dev

Asp.net MVC 5, Identity 2.0 Unable to add a Role to user

分類Dev

Ionic/Angular Auth from ASP.net Membership DB

分類Dev

WCF authentication using ASP.NET Membership Provider

分類Dev

Where are the new membership providers classes in ASP.NET Identity?

分類Dev

Weird Error Upgrading ASP.NET MVC from 4 to 5

分類Dev

ASP.NET MVC4のViewComponent

分類Dev

Calling ASP.NET MVC 4 Controller from Javascript

分類Dev

ASP.NET MVC4 "Getting Started" Configuration Error

分類Dev

Can't add scripts in bundle (asp.net mvc 4)

分類Dev

JSON transmission from the controller to the view in ASP.NET MVC 4

分類Dev

GET MVC 4ASP.NETのURL

分類Dev

ASP.NET MVC 4 error with Google CalendarAPIv3

分類Dev

ASP.net MVC 4 images load locally but not online

分類Dev

ASP.NET MVC4のRegisterRoutes

分類Dev

Set HTML attributes on ASP.Net MVC 4 HTML Helpers?

分類Dev

Using Unity in ASP.NET MVC 4 - Dependency injection error

Related 関連記事

ホットタグ

アーカイブ