自定义.net Identity 2

时间:2018-08-06 01:21:24

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

我有一个带有.net 4.7.2的MVC5应用程序,并且我正在尝试制作自定义版本的.net Identity类。我遵循了本教程中的步骤,并在其中进行了一些更改,因为我的项目依赖于一组WCF服务来与数据库进行通信,因此我没有与数据库的直接连接: overview-of-custom-storage-providers-for-aspnet-identity 因此,主要是我创建了自己的MyUserStore,它实现了IUserStore和IUserRoleStore,并将其添加到Startup.cs中。

public void ConfigureAuth(IAppBuilder app)
{
     app.CreatePerOwinContext<MyUserManager>(MyUserManager.Create);
}

现在的问题是IsInRole方法没有被调用,而且我也遇到了诸如FindByIdAsync,FindByNameAsync和IsInRoleAsync之类的方法的断点,尽管我已经添加了任何东西 [Authorize(Roles ="TestRole")]属性可用于耦合操作,并已尝试显式调用此操作:

this.User.IsInRole("TestRole");

要使它以正确的方式工作,我缺少什么?

编辑#1-添加整个代码: 下面是与用户存储相关的类:

public class MyUserStore :
            IUserStore<MyIdentityUser, int>,
            IUserRoleStore<MyIdentityUser, int>,
            IQueryableUserStore<MyIdentityUser, int>,
            IDisposable
    {

        public Task CreateAsync(MyIdentityUser account)
        {
            // Add account to DB...
            return Task.FromResult<object>(null);
        }

        public Task<MyIdentityUser> FindByIdAsync(int accountId)
        {
            // Get account from DB and return it 
            return Task.FromResult<MyIdentityUser>(account);
        }

        public Task<MyIdentityUser> FindByNameAsync(string userName)
        {
            // Get account from DB and return it 
            return Task.FromResult<MyIdentityUser>(account);
        }

        public Task UpdateAsync(MyIdentityUser account)
        {
            // Update account in DB 
            return Task.FromResult<object>(null);
        }

        public Task AddToRoleAsync(MyIdentityUser account, string roleName)
        {
            throw new NotImplementedException("UserStore.AddToRoleAsync");
            return Task.FromResult<object>(null);
        }

        public Task<IList<string>> GetRolesAsync(MyIdentityUser account)
        {
            // TODO: Check if important to implement
            throw new NotImplementedException("UserStore.GetRolesAsync");
        }

        public Task<bool> IsInRoleAsync(MyIdentityUser account, string task)
        {
            // Return true if has permission (not getting invoked)
            return Task.FromResult<bool>(hasPermission);
        }

         public Task RemoveFromRoleAsync(MyIdentityUser account, string task)
        {
            throw new NotImplementedException("UserStore.RemoveFromRoleAsync");
        }

        public Task DeleteAsync(MyIdentityUser account)
        {
            // Delete user from DB
            return Task.FromResult<Object>(null);
        }    
    }

    public class MyUserRole : IdentityUserRole<int> { }
    public class MyUserClaim : IdentityUserClaim<int> { }
    public class MyUserLogin : IdentityUserLogin<int> { }
    public class MyIdentityUser : IdentityUser<int, MyUserLogin, MyUserRole, MyUserClaim>
    {
        public MyIdentityUser(int id)
        {
            Id = id;
        }

        // My extra account's properties
    }
}

这是UserManager类:

public class MyUserManager : UserManager<MyIdentityUser, int>
{
    public MyUserManager(IUserStore<MyIdentityUser, int> store)
        : base(store) { }

    public static MyUserManager Create(IdentityFactoryOptions<MyUserManager> options, IOwinContext context)
    {
        ///Calling the non-default constructor of the UserStore class
        var manager = new MyUserManager(new MyUserStore());

        manager.UserValidator = new UserValidator<MyIdentityUser, int>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = false;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 3;

        // You can write your own provider and plug it in here.
        return manager;
    }
}

也许这是最重要的部分,登录控制器:

var manager = HttpContext.GetOwinContext().GetUserManager<MyUserManager>();
//check for credentials before sign in ..    
var result = manager.CheckPasswordAsync(vm.userName, vm.password, ref state);
if(result.Result)
{ 
    FormsAuthentication.SetAuthCookie(vm.userName, vm.rememberMe);
    return Redirect("~/");
}

此致

0 个答案:

没有答案