asp.net-mvc ASP.NET Identity - 自定义 UserManager、UserStore

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40501820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:34:14  来源:igfitidea点击:

ASP.NET Identity - custom UserManager, UserStore

asp.net-mvcasp.net-identityasp.net-identity-2

提问by dee zg

I am a bit confused with customizing UserManager and UserStore. Out of the box solution comes with EF implementation and i don't want to use EF but my own DAL that uses MSSQL. I want to have Claims based security where one of the users Claims will be roles.

我对自定义 UserManager 和 UserStore 有点困惑。开箱即用的解决方案带有 EF 实现,我不想使用 EF,而是我自己的使用 MSSQL 的 DAL。我想要基于声明的安全性,其中一个用户声明将是角色。

What i am confused with is the overall process i should do. From what i undestand so far is that i need to make my own

我感到困惑的是我应该做的整个过程。到目前为止我不明白的是我需要自己做

CustomApplicationUser : IUser
CustomUserManager : UserManager<CustomApplicationUser>
CustomUserStore : IUserStore, IUserClaimStore

Questions:

问题:

  1. Am i on the right track with this?
  2. I want to use IsInRole() method on my CustomUserManager but not sure how to do it with Claims. I am aware there is IUserRoleStore.IsInRole() which default UserManager calls in UserManager.IsInRole() but i don't want separate Roles table in my DB. What i want is Claims DB table with one of ClaimType being Role and that UserManager.IsInRole() uses that. Now, i am not evet sure why would i ever need UserManager.IsInRole() method? Would i actually need to have something like custom ClaimsIdentity SignInManager.CreateUserIdentityAsync() and within that one call my own implementation of filling in all users info including Claims?
  1. 我在正确的轨道上吗?
  2. 我想在我的 CustomUserManager 上使用 IsInRole() 方法,但不确定如何使用 Claims。我知道有 IUserRoleStore.IsInRole() 默认 UserManager 调用 UserManager.IsInRole() 但我不想在我的数据库中使用单独的 Roles 表。我想要的是 Claims DB 表,其中一个 ClaimType 是 Role 并且 UserManager.IsInRole() 使用它。现在,我不确定为什么我需要 UserManager.IsInRole() 方法?我实际上是否需要像自定义 ClaimsIdentity SignInManager.CreateUserIdentityAsync() 这样的东西,并在其中调用我自己的填充所有用户信息(包括 Claims)的实现?

It seems a bit confusing for me and i can't seem to find some clear documentation about it so if anyone could shed a bit of light on it i would highly appreciate it!

这对我来说似乎有点混乱,我似乎无法找到一些关于它的明确文档,所以如果有人能对此有所了解,我将不胜感激!

采纳答案by Dawid Rutkowski

Instead of just copying I will just point you to following article: Overview of Custom Storage Providers for ASP.NET Identity.

我将不只是复制,而是将您指向以下文章:ASP.NET Identity 的自定义存储提供程序概述

Take a look at this, it should give you nice overview of how identity works in ASP.NET. It's also good for choosing what you want to override and customize in your application.

看看这个,它应该让您很好地了解身份在 ASP.NET 中的工作方式。它也适用于在应用程序中选择要覆盖和自定义的内容。