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

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

问题描述

所有我的应用程序的认证和授权过程是使用存储过程实现。我写了一个类的所有功能中,我需要,如 GetUsers 登录 AddRole 使用addMember 等。 此外,用于管理用户和角色和权限管理页面,通过使用这个类完成的。

我只需要添加验证(我的意思是批准属性),饼干登录和注销和存储一些服务器端的数据为每个登录。我想我需要实施身份是什么?

在这种情况下,可以请你指导我与它的实现?看来你需要做的很基本的东西是实施创建方法传递 IUserStore 的实例给构造函数。但是,我并不需要有任何表用户或角色,我怎么能实现这个方法?

这是当前类,并请让我知道如果你需要看使用存储过程我自定义的验证类。

 公共类AppUserManager:的UserManager< APPUSER>
{
    公共AppUserManager(IUserStore< APPUSER>存储):基地(店){}
    公共静态AppUserManager创建(IdentityFactoryOptions< AppUserManager>选项,IOwinContext上下文)
    {
        // AppUserManager经理=新AppUserManager();
        //返回经理;
        返回null;
    }
}
 

解决方案

随着 alisabzevari 建议你要实现你的< A HREF =htt​​ps://msdn.microsoft.com/en-us/library/dn613278(v=vs.108).aspx相对=nofollow> IUserStore 。
你甚至不依赖于定义的存储和表结构。 您可以自定义存储层的每一个位。

我做了一些实验,并试图实现自己的UserManager RoleManager 使用不同的存储,如< A HREF =htt​​ps://github.com/xivSolutions/biggy相对=nofollow> Biggy :

  

一个基于文件的文档存储为.NET。

您可以找到code 这里在GitHub上。

首先要做的是实现你的的UserManager 在这里你可以配置你的密码验证的要求:

 公共类AppUserManager:的UserManager&LT; APPUSER,INT&GT;
{
    公共AppUserManager(IUserStore&LT; APPUSER,INT&GT;存储):基地(店)
    {
        this.UserLockoutEnabledByDefault = FALSE;
        // this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(10);
        // this.MaxFailedAccessAttemptsBeforeLockout = 10;
        this.UserValidator =新UserValidator&lt;使用者,INT&GT;(本)
        {
        AllowOnlyAlphanumericUserNames =假,
        RequireUniqueEmail = FALSE
        };

        //配置密码验证逻辑
        this.PasswordValidator =新PasswordValidator
        {
        RequiredLength = 4,
        RequireNonLetterOrDigit =假,
        RequireDigit =假,
        RequireLowercase =假,
        RequireUppercase =假,
        };
    }
}
 

,然后定义你的 IUserStore 实施。你必须实现的主要方法是 CreateAsync

 公共System.Threading.Tasks.Task CreateAsync(用户用户)
{
    //保存在存储用户。
    返回Task.FromResult(用户);
}
 

这将收到 IUSER ,你必须坚持你的自定义存储并将其返回。

如果你看一下在code我已经的实施你可以看到我用了几个接口 IUserRoleStore IUserPasswordStore IUserClaimStore 因为我需要使用的角色和索赔等等等等。

我也实现我的自己 SignInManager

一旦定义了所有的实现,你可以引导一切在<一个href="https://github.com/Leftyx/AspNetIdentityCustomDb/blob/master/AspNetIdentityCustomDb/App_Start/Startup.Auth.cs"相对=nofollow>启动:

  app.CreatePerOwinContext&LT; Custom.Identity.UserManager&GT;(()=&gt;新建Custom.Identity.UserManager(新Custom.Identity.UserStore(folderStorage)));
app.CreatePerOwinContext&LT; Custom.Identity.RoleManager&GT;(()=&gt;新建Custom.Identity.RoleManager(新Custom.Identity.RoleStore(folderStorage)));
app.CreatePerOwinContext&LT; Custom.Identity.SignInService&GT;((选项,上下文)=&gt;新建Custom.Identity.SignInService(context.GetUserManager&LT; Custom.Identity.UserManager&GT;(),context.Authentication));
 

您可以检查我的<一个href="https://github.com/Leftyx/AspNetIdentityCustomDb/blob/master/AspNetIdentityCustomDb/Controllers/AccountController.cs#L78"相对=nofollow>的AccountController ,我尝试验证用户:

  VAR的结果=等待SignInManager.PasswordSignInAsync(model.Email,model.Password,model.RememberMe,shouldLockout:假);
开关(结果)
{
案例SignInStatus.Success:
    返回RedirectToLocal(RETURNURL);
案例SignInStatus.LockedOut:
    返回视图(锁定);
案例SignInStatus.RequiresVerification:
    返回RedirectToAction(送code,新{RETURNURL = RETURNURL,了rememberMe = model.RememberMe});
案例SignInStatus.Failure:
默认:
    ModelState.AddModelError(,无效的登录尝试。);
    返回查看(模型);
}
 

PasswordSignInAsync 被称为你会发现几个你的的UserManager 将被调用的方法。第一个将 FindByNameAsync

 公共System.Threading.Tasks.Task&lt;使用者&GT; FindByNameAsync(用户名字符串)
{
    //使用用户名获取你的用户。
    返回Task.FromResult(用户);
}
 

您必须执行存储过程,我想,在这里您可以获取从数据库中的用户。

然后另一种方法 FindByIdAsync 将的

 公共System.Threading.Tasks.Task&lt;使用者&GT; FindByIdAsync(INT用户id)
{
    //获取 - 再 - 从数据库的标识用户。
    返回Task.FromResult(用户);
}
 

此外,你必须使用你的存储过程中由他/她的ID找到你的用户。

如果你从github上下载我的项目和玩它,你会发现大多数的这些方法会被多次调用。不要害怕。它就是这样儿的。

我会建议你插入断点在的 UserStore ,看看一切结合在一起的。

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.

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?

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

解决方案

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.

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

A File-based Document Store for .NET.

You can find the code here on GitHub.

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

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

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

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.

I've also implemented my own SignInManager.

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

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

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.

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

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

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.

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

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

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