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

查看:28
本文介绍了如何在 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 中,您有一个注册操作,如下所示:

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天全站免登陆