分析器错误消息:无法从程序集

时间:2015-09-06 05:15:07

标签: asp.net-mvc-4

我想为我的应用程序添加roleprovider,但由于解析器错误,我遇到了麻烦。

以下是我的文件:

enter image description here

这是我的网络配置:

<roleManager defaultProvider ="roleProvider" enabled="true">
    <providers>
      <add name="roleprovider"  type="MvcApplication6.roleprovider,MvcApplication6"/>
    </providers>
  </roleManager>

我继续得到Parser Error Message: Could not load type 'roleprovider' from assembly 'MvcApplication6'

我尝试了几种方法,例如添加到App_code

如何解决此问题?我尝试了其他解决方案,但它们对我不起作用。我可能做错了所以请帮忙。

1 个答案:

答案 0 :(得分:1)

您可以创建一个名为Helper的文件夹,然后尝试:

<roleManager cacheRolesInCookie="true" defaultProvider="CustomRoleProvider" enabled="true">
      <providers>
        <clear />
        <add name="CustomRoleProvider" type="MvcApplication6.Helper.CustomRoleProvider"/>
      </providers>
</roleManager>

还有一件事:确保以正确的方式实现CustomRoleProvider: 使用系统; 使用System.Web.Security;

namespace MvcApplication6.Helper
{
    public class CustomRoleProvider : RoleProvider
    {
        public override bool IsUserInRole(string username, string roleName)
        {
            if (username.ToLowerInvariant() == "test" && roleName.ToLowerInvariant() == "User")
                return true;
            // blabla ...
            return false;
        }

        public override string[] GetRolesForUser(string username)
        {
            if (username.ToLowerInvariant() == "test")
            {
                return new[] { "User", "Helpdesk" };
            }

            if(username.ToLowerInvariant()=="test2")
            {
                return new [] { "Admin" };
            }

            return new string[] { };
        }

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

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                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[] GetUsersInRole(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();
        }
    }
}