ASP.Net身份授权不起作用

时间:2014-09-26 10:06:41

标签: c# asp.net-mvc identity authorize

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

使用授权的控制器方法:

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

带有dbcontext的AppModel.cs

       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; }
    }


}

IndentityConfig.cs

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);
            }
        }

    }
}

0 个答案:

没有答案