实现 UserManager 以使用自定义类和存储过程 [英] Implementing UserManager to use a custom class and Stored Procedures

查看:20
本文介绍了实现 UserManager 以使用自定义类和存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的所有身份验证和授权过程都是使用存储过程完成的.我已经编写了一个包含我需要的所有功能的类,例如GetUsersLoginAddRoleAddMember 等.用于管理用户、角色和权限的管理页面也是通过使用这个类来完成的.

All of the authentication and authorization process of my app is done using stored procedures. I've written a class with all of functionalities that I need, e.g. GetUsers, Login, AddRole, AddMember, etc. Also the admin page for managing users and roles and permissions is done by using this class.

我只需要添加 authentication(我的意思是 authorize 属性)、用于登录和注销的 cookie 以及为每个登录存储一些服务器端数据.我想我需要为此实现 Identity?

I just need to add authentication (I mean that authorize attribute), cookies for Login and Logout and storing some server-side data for each Login. I think I need to implement Identity for that?

在这种情况下,您能指导我实施吗?看起来您需要做的非常基本的事情是实现一个 create 方法,该方法将 IUserStore 的实例传递给构造函数.但是我不需要任何用户或角色的表,我该如何实现这种方法?

In that case, can you please guide me with its implementation? It seems the very basic thing you need to do is to implement a create method that passes an instance of IUserStore to the constructor. But I don't need to have any tables for users or roles, how can I implement this method?

这是当前类,如果您需要查看我使用存储过程的自定义身份验证类,请告诉我.

This is the current class, and please let me know if you need to see my custom authentication class that uses stored procedures.

public class AppUserManager : UserManager<AppUser>
{
    public AppUserManager(IUserStore<AppUser> store) : base(store) { }
    public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
    {
        //AppUserManager manager = new AppUserManager();
        //return manager;
        return null;
    }
}

推荐答案

因为 alisabzevari 建议您必须实施您的 IUserStore.
您甚至不依赖于定义的存储和表结构.您可以自定义存储层的每一位.

As alisabzevari suggested you have to implement your IUserStore.
You do not even depend on the storage and table structure defined. You can customize every single bit of your storage layer.

我做了一些实验并尝试使用不同的存储来实现我自己的 UserManagerRoleManager,例如 Biggy:

I did some experiments and tried to implement my own UserManager and RoleManager using a different storage, such as Biggy:

用于 .NET 的基于文件的文档存储.

A File-based Document Store for .NET.

您可以在 GitHub 上此处找到代码.

You can find the code here on GitHub.

首先要做的是实现您的 UserManager,您可以在其中配置密码验证的要求:

First thing to do is to implement your UserManager where you can configure the requirements for your password validation:

public class AppUserManager : UserManager<AppUser, int>
{
    public AppUserManager (IUserStore<AppUser, int> store): base(store)
    {
        this.UserLockoutEnabledByDefault = false;
        // this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(10);
        // this.MaxFailedAccessAttemptsBeforeLockout = 10;
        this.UserValidator = new UserValidator<User, int>(this)
        {
        AllowOnlyAlphanumericUserNames = false,
        RequireUniqueEmail = false
        };

        // Configure validation logic for passwords
        this.PasswordValidator = new PasswordValidator
        {
        RequiredLength = 4,
        RequireNonLetterOrDigit = false,
        RequireDigit = false,
        RequireLowercase = false,
        RequireUppercase = false,
        };
    }
}

然后定义您的 IUserStore 实施.您必须实现的主要方法是 CreateAsync:

and then define your IUserStore implementation. The main method you must implement is CreateAsync:

public System.Threading.Tasks.Task CreateAsync(User user)
{
    // Saves the user in your storage.
    return Task.FromResult(user);
}

它将收到一个 IUser,您必须将其保存在自定义存储中并返回.

it will receive an IUser which you have to persist in your custom storage and return it.

如果你看看我的代码实施你可以看到我使用了一些接口IUserRoleStoreIUserPasswordStoreIUserClaimStore 等,因为我需要使用角色和索赔.

If you have a look at the code I've implemented you can see I've used a few interfaces IUserRoleStore, IUserPasswordStore, IUserClaimStore etc etc as I needed to use roles and claims.

我还实现了我的自己的登录管理器.

一旦你定义了你的所有实现,你就可以在 启动:

Once you've defined all your implementation you can bootstrap everything at startup:

app.CreatePerOwinContext<Custom.Identity.UserManager>(() => new Custom.Identity.UserManager(new Custom.Identity.UserStore(folderStorage)));
app.CreatePerOwinContext<Custom.Identity.RoleManager>(() => new Custom.Identity.RoleManager(new Custom.Identity.RoleStore(folderStorage)));
app.CreatePerOwinContext<Custom.Identity.SignInService>((options, context) => new Custom.Identity.SignInService(context.GetUserManager<Custom.Identity.UserManager>(), context.Authentication));

您可以查看我的 AccountController我尝试验证用户的地方:

You can check my AccountController where I try to validate the user:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
    return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
    return View("Lockout");
case SignInStatus.RequiresVerification:
    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
    ModelState.AddModelError("", "Invalid login attempt.");
    return View(model);
}

一旦 PasswordSignInAsync 被调用,您会注意到 UserManager 的一些方法将被调用.第一个是 FindByNameAsync:

Once PasswordSignInAsync is called you will notice a few of the methods of your UserManager will be called. The first one will be FindByNameAsync:

public System.Threading.Tasks.Task<User> FindByNameAsync(string userName)
{
    //Fetch your user using the username.
    return Task.FromResult(user);
}

我猜你必须实现你的存储过程,你将从数据库中获取你的用户.

You will have to implement your stored procedure, I guess, where you'll fetch your user from the DB.

然后另一种方法 FindByIdAsync 将是 调用:

Then another method FindByIdAsync will be called:

public System.Threading.Tasks.Task<User> FindByIdAsync(int userId)
{
    // Fetch - again - your user from the DB with the Id.
    return Task.FromResult(user);
}

同样,您必须使用您的存储过程通过他/她的 id 找到您的用户.

Again you'll have to use your stored procedure to find your user by his/her id.

如果您从 github 下载我的项目并使用它,您会注意到大多数方法将被多次调用.不要害怕.它就是这样儿的.

If you download my project from github and play around with it you'll notice that most of those methods will be called multiple times. Don't get scared. That's the way it is.

我建议您在 UserStore 并查看所有内容如何组合在一起.

I would suggest you to insert breakpoints in every single method of the UserStore and see how everything fits together.

这篇关于实现 UserManager 以使用自定义类和存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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