缺少自定义角色提供程序命

时间:2013-08-25 21:09:29

标签: c# asp.net-mvc

我正在尝试编写自己的角色提供程序,但是,它说缺少命名空间,我做错了什么。我正在c#mvc visual studio 2012中编码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security.RoleProvider; //this gave me an error
using MyProject.Models;

namespace MyProject.Controllers
{
    public class MyRoleProvider : RoleProvider
    {
        //
        // GET: /RoleProvider/
        private DefaultConnection db = new DefaultConnection();

        public ActionResult Index()
        {
            return View();
        }
        public override bool IsUserInRole(string username, string roleName)
        {
            // Return status defaults to false
            bool ret = false;

            if (RoleExists(roleName))
            {
                {
                    int c = (from m in db.Users
                             where m.userid == username &&
                             m.role == roleName
                             select m).Count();

                    if (c > 0)
                        ret = true;
                }
            }
            return ret;
        }
        public override bool RoleExists(string roleName)
        {
            bool ret = false;

            // If the specified role doesn't exist
            if (!GetAllRoles().Contains(roleName))
                ret = true;

            return ret;
        }
        public override string[] GetAllRoles()
        {
            string[] roles = null;
            roles = (from roleadmin in db.Users
                     select roleadmin.userid).ToArray();
            return roles;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

只需使用

using System.Web.Security;

而不是将类名添加到使用中。

相关问题