0

我正在编写一个使用 Asp.net MVC 5 和身份的小应用程序。我遇到了授权属性不起作用的问题,即指定的角色不被视为授权角色。我已经尝试过其他类似问题的帖子的建议,但没有运气。欢迎任何帮助。

使用授权的控制器方法:

    // GET: /ToDo/All
    [Authorize(Roles="Boss")]
    public async Task<ActionResult> All()
    {
        return View(await db.ToDoes.ToListAsync());
    }

AppModel.cs 与 dbcontext

       using System;
       using System.Collections.Generic;
         using System.Data.Entity;
using System.Linq;
using System.Web;
using Microsoft.AspNet.Identity.EntityFramework;
using System.ComponentModel.DataAnnotations;

namespace AspIdentityTrial.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string HomeTown { get; set; }
        public virtual ICollection<ToDo> ToDoes { get; set; }

        public virtual MyUserInfo MyUserInfo { get; set; }
    }

    public class MyUserInfo
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class ToDo
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public bool IsDone { get; set; }
        public virtual ApplicationUser User { get; set; }
    }

    public class TrialDbContext : IdentityDbContext<ApplicationUser>
    {
        public TrialDbContext()
            : base("DefaultConnection")
        {
            this.Configuration.LazyLoadingEnabled = true;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Change the name of the table to be Users instead of AspNetUsers
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users");
            modelBuilder.Entity<ApplicationUser>()
                .ToTable("Users");
        }


        public DbSet<ToDo> ToDoes { get; set; }

        public DbSet<MyUserInfo> MyUserInfo { get; set; }
    }


}

身份配置文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AspIdentityTrial.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using Microsoft.AspNet.Identity.Owin;
using System.Threading;
using System.Threading.Tasks;

namespace AspIdentityTrial.App_Start
{
    public class TrialDbInitializer : DropCreateDatabaseAlways<TrialDbContext>
    {
        protected override void Seed(TrialDbContext context)
        {
            InitializeIdentifyForEF(context);
            base.Seed(context);
        }

        private void InitializeIdentifyForEF(TrialDbContext context)
        {
            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            var myinfo = new MyUserInfo() {FirstName = "Mat", LastName = "Z"};
            string name = "Boss";
            string password = "123456";
            string test = "test";

            RoleManager.Create(new IdentityRole(test));
            UserManager.Create(new  ApplicationUser() { UserName = test});

                        //Create Role Admin if it does not exist
            if (!RoleManager.RoleExists(name))
            {
                var roleresult = RoleManager.Create(new IdentityRole(name));
            }

            //Create User=Admin with password=123456
            var user = new ApplicationUser();
            user.UserName = name;
            user.HomeTown = "Seattle";
            user.MyUserInfo = myinfo;
            var adminresult = UserManager.Create(user, password);

            //Add User Admin to Role Admin
            if (adminresult.Succeeded)
            {
                var result = UserManager.AddToRole(user.Id, name);
            }
        }

    }
}
4

0 回答 0