标签: asp.net-core-identity

恢复AspNet标识核心包时出错

使用MS VS 2015 Pro和Asp.net Core,我正在尝试添加Microsoft.AspNet.Identity.Core包,但是我收到此错误:

   Errors in c:\users\xxx\documents\visual studio 2015\Projects\WebApplication1\src\WebApplication1\WebApplication1.xproj
        Package Microsoft.AspNet.Identity.Core 2.2.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.AspNet.Identity.Core 2.2.0 supports: net45 (.NETFramework,Version=v4.5)
        One or more packages are incompatible with .NETCoreApp,Version=v1.0.
Run Code Online (Sandbox Code Playgroud)

我还将Packege版本更改为2.1.但得到了同样的错误.你能告诉我为什么会这样吗?

asp.net-identity asp.net-core asp.net-core-identity

6
推荐指数
1
解决办法
1121
查看次数

将标识移动到类库ASP.Net Core

我开发了一个基于ASP.Net核心的新项目.我已将所有EF代码(模型,映射,DbContext)移动到专用DAL类库中,以遵循SOLID规则的单一责任原则.

但是,我现在需要在我的项目中添加身份验证,并且需要将以下内容添加到我的Web项目的Startup.cs中,如不同的教程所示:

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

它将涉及添加Microsoft.AspNetCore.Identity.EntityFrameworkCore包,在我看来,我开始通过将此包包含到我的Web项目中来打破SRP规则.

是否可以将所有身份代码(服务,模型)作为外部类库移动,就像我为DAL所做的那样.

entity-framework-core asp.net-core asp.net-core-identity

6
推荐指数
1
解决办法
3325
查看次数

EntityFrameworkCore Identity 的继承问题

我最近决定在我使用 ASP.NET Core MVC 开发的网站上实现 ASP.NET Identity 功能。

让我们快速浏览一下主题中的表和类:

public class User : IdentityUser
{
    public string FirstName { get; set; }
    public stirng LastName { get; set; }
    public int CountryId { get; set; }

    public Country Country { get; set; }
}

public class Country 
{
    public int Id { get; set; }
    public string ISO { get; set; }
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Country班是有点风马牛不相及,但如果你想从参考User类。

通过这个设置,我已经像这样配置了我的数据库上下文:

public class DatabaseContext : …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-identity asp.net-core-mvc asp.net-core-identity

6
推荐指数
1
解决办法
833
查看次数

userManager.FindByName不返回角色

我正在使用OpenIddict进行令牌身份验证.昨天当我打电话给userManager.FindByNameAsync(request.Username)我时,我得到了带角色的用户.
今天我得到Roles属性count = 0的用户.

我试图加载角色,await userManager.GetRolesAsync(user);我得到数量为3的数组.这意味着用户有角色.

我不知道改变了什么,但是如何使用FindByNameAsync函数加载具有角色的用户?

完整代码:

[HttpPost("token"), Produces("application/json")]
public async Task<IActionResult> Exchange(OpenIdConnectRequest request)
{
    Debug.Assert(request.IsTokenRequest(),
        "The OpenIddict binder for ASP.NET Core MVC is not registered. " +
        "Make sure services.AddOpenIddict().AddMvcBinders() is correctly called.");

    if (request.IsPasswordGrantType())
    {
        var user = await userManager.FindByNameAsync(request.Username);  //roles count is 0
        if (user == null)
        {
            return BadRequest(new OpenIdConnectResponse
            {
                Error = OpenIdConnectConstants.Errors.InvalidGrant,
                ErrorDescription = "The email/password couple is invalid."
            });
        }
        var roles = await userManager.GetRolesAsync(user);  //roles …
Run Code Online (Sandbox Code Playgroud)

asp.net-identity asp.net-core openiddict asp.net-core-identity

6
推荐指数
1
解决办法
1505
查看次数

ASP.NET Core Identity 模拟特定用户

假设我有以下角色:

  1. 行政

  2. 用户

我希望 Admin 角色模拟具有 User 角色的特定用户帐户,但不知道该特定用户帐户的密码。

管理员应该能够模拟应用程序中的任何用户,并能够以用户自己的身份浏览应用程序。 我找到了一个链接,其中实际上是在 ASP.NET MVC 4.6 中实现的,但是在将其转换为 Core 版本时有点头疼。

主要是因为链接中的最后一行代码

authenticationManager.SignIn(new AuthenticationProperties() 
{ IsPersistent = false }, impersonatedIdentity);
Run Code Online (Sandbox Code Playgroud)

SignIn.NET Core 中的where参数不允许IdentityResult再传递类 (impersonatedIdentity)。它现在只能采取ClaimsPrincipal

所以我最终做的是这个,

public async Task<IActionResult> ImpersonateUserAsync(string userName)
    { 
        var impersonatedUser = await _userManager.FindByNameAsync(userName);         

        var claims = new List<Claim> {
            new Claim(ClaimTypes.Name, impersonatedUser.FirstName, ClaimValueTypes.String),
            new Claim(ClaimTypes.Surname, impersonatedUser.LastName, ClaimValueTypes.String),
            new Claim(ClaimTypes.Email, impersonatedUser.Email, ClaimValueTypes.String)
        };

        var user = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));

        var authenticationManager = _httpContextAccessor.HttpContext.Authentication;
        await authenticationManager.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        await …
Run Code Online (Sandbox Code Playgroud)

c# impersonation claims-based-identity asp.net-core-identity asp.net-core-1.1

6
推荐指数
1
解决办法
5226
查看次数

如何确认ASP.Net Core 1.1MVC中的电话号码

我无法弄清楚如何在asp.net核心1.1中进行电话号码确认

身份服务配置包含要求确认的电子邮件和/或电话号码的明确选项.

它可以通过以下方式完成:

services
    .AddIdentity<User, Role>(options =>
    {
        options.SignIn.RequireConfirmedEmail = true;
        options.SignIn.RequireConfirmedPhoneNumber = true;
     });
Run Code Online (Sandbox Code Playgroud)

由于UserManager包含显式令牌生成器及其验证器,因此电子邮件的验证非常简单:

var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
Run Code Online (Sandbox Code Playgroud)

生成的令牌可以通过以下方式验证:

var result = await _userManager.ConfirmEmailAsync(user, code);
Run Code Online (Sandbox Code Playgroud)

如果令牌有效,上面的行将把user.EmailConfirmed标志切换为true.

现在的问题是我没有看到类似的方法来生成电话验证令牌及其等效方法来验证它(如果成功,它应该将user.PhoneNumberConfirmed标志切换为true).

但是,用户管理器包含的用户手机更改方法很少:

_userManager.GenerateChangePhoneNumberTokenAsync();
Run Code Online (Sandbox Code Playgroud)

_userManager.VerifyChangePhoneNumberTokenAsync();
Run Code Online (Sandbox Code Playgroud)

但似乎这些方法不会切换user.PhoneNumberConfirmed标志.

我错过了什么吗?确认用户电话号码的正确方法是什么(换句话说,将user.PhoneNumberConfirmed设置为true)?

asp.net-core-mvc asp.net-core asp.net-core-identity

6
推荐指数
1
解决办法
2946
查看次数

在Asp.Net Core Identity中要求唯一的电话号码

在Asp.Net Core Identity框架中,通过设置RequireUniqueEmail = true,我可以轻松地要求一个唯一的电子邮件地址。

有什么办法可以对用户的电话号码执行相同的操作?请注意,我不想要求输入经过确认的电话号码。不需要用户输入电话号码,但是如果输入,则它必须是唯一的。

unique asp.net-core asp.net-core-identity

6
推荐指数
2
解决办法
1162
查看次数

IdentityServer4对基于角色的Web API授权

我使用IdentityServer4(v2.2.1)与.Net Core 2.0和Asp.Net核心身份.我的解决方案中有三个项目.

  1. IdentityServer
  2. Web API
  3. MVC Web应用程序

我正在尝试在我的Web API上实现基于角色的授权,以便任何客户端都可以将访问令牌传递给Web API以访问资源.

目前我可以在MVC应用程序控制器上实现Roles基础授权,但我无法为WEB API Controller传递/配置相同的权限.

以下是Identity Server Files:Config.cs

public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            //SCOPE - Resource to be protected by IDS 
            new ApiResource("TCSAPI", "TCS API")
            {
                UserClaims = { "role" }
            }
        };
    }


 public static IEnumerable<Client> GetClients()
    {
        return new List<Client>
        {
new Client
            {
                ClientId = "TCSIdentity",
                ClientName = "TCS Mvc Client Application .",
                AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                RequireConsent = false,
                ClientSecrets =
                {
                    new Secret("secret".Sha256()) …
Run Code Online (Sandbox Code Playgroud)

identityserver4 asp.net-core-identity

6
推荐指数
1
解决办法
3927
查看次数

为什么在 asp .net core 2.1 中的每次迁移时都会删除种子角色?

我在每次迁移时都会遇到种子角色的奇怪行为。无论您做了什么更改,迁移都会删除种子角色并再次插入它们。当项目中没有进行任何修改时,将创建下面给出的迁移。

所有其他模型均已正确播种,并且仅在修改它们时才考虑迁移。

我将 ASP .NET Core 2.1 与个人身份验证一起使用

用于播种的 DbContext 类

 public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        #region Seed-Roles

        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "SuperAdmin", NormalizedName = "SuperAdmin".ToUpper() });
        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Owner", NormalizedName = "Owner".ToUpper() });
        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Tester", NormalizedName = "Tester".ToUpper() });
        modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "User", NormalizedName …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-identity asp.net-core-2.1 ef-core-2.1 entity-framework-migrations

6
推荐指数
2
解决办法
1090
查看次数

使IdentityServer和应用程序之间的数据保持同步

我将IdentityServer4(具有Asp.Net Core身份)用作多个应用程序的集中式身份验证点。

在一个应用程序中,我想设置一个计划的作业,以向多个用户发送电子邮件通知。当作业将执行时,它将无法访问用户声明(因为它将不在单个用户请求的上下文中执行),因此将没有地方读取用户的电子邮件。这意味着我将不得不在应用程序数据库中复制电子邮件。

但是,如果某些用户想要更改应用程序和IdentityServer之间的电子邮件,该如何使其保持同步?

synchronization oauth-2.0 identityserver4 asp.net-core-identity

6
推荐指数
2
解决办法
278
查看次数