public override string [] GetRolesForUser(string username)

时间:2016-07-29 00:51:21

标签: asp.net-mvc-4 authentication authorization

你好这里是我的问题,我在我的数据库下创建身份验证,所以当我把[Authorize(Roles =“admin”)]它会被我的程序读取我已经在我的web.config中设置了在创建类,但它仍然指导我在我的登录页面默认未授权时,请帮助

这是我的代码

MyRoleProvider.cs

public override string[] GetRolesForUser(string username)
        {
        UserDbEntities OE = new UserDbEntities();
        string s = OE.UserAccounts.Where(x => x.Username == username).FirstOrDefault().Role;
        string[] resultS = {s};
        return resultS; 
    }

控制器

 public class CustomerController : Controller
    {
    private UserDbEntities db = new UserDbEntities();

    // GET: Customer
    [Authorize(Roles="admin")]
    public ActionResult Index()
    {
        return View(db.Customers.ToList());
    }

Web.config

 <authentication mode="Forms">
          <forms loginUrl="Account/Login"></forms>
        </authentication>
        <roleManager enabled="true" defaultProvider="MyProvider">
          <providers>
          <clear/>
        <add name ="MyProvider" type="Sample.MyRoleProvider"/>
      </providers>   
    </roleManager>

0 个答案:

没有答案