如何在Identity 2.0中禁用用户? [英] How to disable a User in Identity 2.0?

查看:152
本文介绍了如何在Identity 2.0中禁用用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来禁用Identity 2.0中的用户,并且似乎找不到任何信息.

I'm trying to find a way to disable a user in Identity 2.0 and can't seem to find any info on it.

我想基本上将用户设置为IsActive = false,并且希望在用户创建后立即进行操作.但是,我需要一种为网站管理员设置IsActive的方法.我已经拥有ASP.Net成员资格,但是我希望将该网站隐瞒MVC和Identity.

I would like to basically set a user to IsActive=false and would prefer to do it as soon as the user is created. However, I need a way to set the IsActive for our site Admin. I already have this with ASP.Net membership but I'm looking to covert the site to MVC and Identity.

根据我的要求,我们要求人们继续注册一个帐户,但我们希望默认情况下将其禁用.然后,当我们收到加入费用时,我们将返回并启用它们.我们还使用它来禁用用户订阅并且尚未续订的用户.

For my requirements we ask people to go ahead and register an account but we want it to be disabled by default. Then when we receive payment for joining we will go back and enable them. We also use it to disable users when their subscription is up and they haven't renewed.

是否有一种方法可以禁用一个帐户而不删除它或将其锁定X倍的时间?到目前为止,我还没有找到任何一种方法来仅禁用Identity中的用户,并且令我惊讶的是,这个问题从未出现过.

Is there a way to disable an account without deleting it or only locking them out for an X amount of time? So far I haven't found any way of just disabling a user in Identity and I'm surprised this question hasn't come up before.

推荐答案

在创建安装了Identity位的站点时,您的站点将具有一个名为"IdentityModels.cs"的文件.在此文件中,有一个名为ApplicationUser的类,该类继承自IdentityUser.

When you create a site with the Identity bits installed, your site will have a file called "IdentityModels.cs". In this file is a class called ApplicationUser which inherits from IdentityUser.

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser

其中的注释中有一个不错的链接,为方便起见,请单击此处

There is a nice link in the comments there, for ease click here

本教程确切地告诉您为用户添加自定义属性所需执行的操作.

This tutorial tells you exactly what you need to do to add custom properties for your user.

实际上,甚至不必费心看本教程.

And actually, don't even bother looking at the tutorial.

1)将属性添加到ApplicationUser类,例如:

1) add a property to the ApplicationUser class, eg:

public bool? IsEnabled { get; set; }

2)在数据库的AspNetUsers表上添加一个具有相同名称的列.

2) add a column with the same name on the AspNetUsers table in your DB.

3)繁荣,就是这样!

3) boom, that's it!

现在,在AccountController中,您将具有一个Register操作,如下所示:

Now in your AccountController, you have a Register action as follows:

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, IsEnabled = true };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)

我在创建ApplicationUser对象时添加了IsEnabled = true.现在,该值将保留在AspNetUsers表的新列中.

I've added the IsEnabled = true on the creation of the ApplicationUser object. The value will now be persisted in your new column in the AspNetUsers table.

然后,您需要通过覆盖ApplicationSignInManager中的PasswordSignInAsync来处理此值的检查,这是登录过程的一部分.

You would then need to deal with checking for this value as part of the sign in process, by overriding PasswordSignInAsync in ApplicationSignInManager.

我这样做如下:

public override Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool rememberMe, bool shouldLockout)
    {
        var user = UserManager.FindByEmailAsync(userName).Result;

        if ((user.IsEnabled.HasValue && !user.IsEnabled.Value) || !user.IsEnabled.HasValue)
        {
            return Task.FromResult<SignInStatus>(SignInStatus.LockedOut);
        }

        return base.PasswordSignInAsync(userName, password, rememberMe, shouldLockout);
    }

您的里程可能会有所不同,并且您可能不希望返回该SignInStatus,但您会明白的.

Your mileage may vary, and you may not want to return that SignInStatus, but you get the idea.

这篇关于如何在Identity 2.0中禁用用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆