从身份扩展属性的问题(ASP.NET Core) [英] Problems extending properties from Identity (ASP.NET Core)

查看:63
本文介绍了从身份扩展属性的问题(ASP.NET Core)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从Identity扩展属性.

I'm having trouble extending properties from Identity.

这样做的全部原因是使我的员工数据库与应用程序数据库(包括身份信息)集成在一起,并将其用作一个大型数据库.

The whole reason for this is to have my employee database integrated with the application database (which includes the identity stuff) and use it as one big database.

我尝试遵循此 answer ,但似乎他们正在使用另一版本的ASP.NET.我正在使用ASP.NET Core,版本:2.0.3

I tried following this answer, but seems like they are using another version of ASP.NET. I'm using ASP.NET Core, Version: 2.0.3

这是我的 ApplicationUser.cs 文件的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace src.Models
{
    public class ApplicationUser : IdentityUser
    {

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {

            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            userIdentity.AddClaim(new Claim("FirstName", this.FirstName.ToString()));
            userIdentity.AddClaim(new Claim("LastName", this.LastName.ToString()));
            userIdentity.AddClaim(new Claim("JobTitle", this.JobTitle.ToString()));
            userIdentity.AddClaim(new Claim("Status", this.Status.ToString()));

            return userIdentity;
        }

        string FirstName { get; set; }
        string LastName { get; set; }
        string JobTitle { get; set; }
        string Status { get; set; }
        int Age { get; set; }
    }
}

我在 CreateIdentityAsync 上遇到错误,错误为:

I'm getting an error on CreateIdentityAsync , with the error:

'UserManager<ApplicationUser>' does not contain a definition for 'CreateIdentityAsync' 
and no extension method 'CreateIdentityAsync' accepting a first argument of type 
'UserManager<ApplicationUser>' could be found (are you missing a using directive 
or an assembly reference?) [src]

以及 DefaultAuthenticationTypes 上的错误,错误:

and an error on DefaultAuthenticationTypes, the error:

The name 'DefaultAuthenticationTypes' does not exist in the current context [src]

使用ASP.NET Core无法做到这一点,还是我做错了什么?

Is this not possible with ASP.NET Core, or that I'm doing something wrong?

推荐答案

我自己弄清楚了.我尝试使用ASP.NET Core做不到的事情.我不得不做其他事情.

I figured it out myself. What I was trying to do was not possible with ASP.NET Core. I had to do something else.

我创建了一个AppClaimsPrincipalFactory.cs文件

I created a AppClaimsPrincipalFactory.cs file

public class AppClaimsPrincipalFactory : UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
{
    public AppClaimsPrincipalFactory(
        UserManager<ApplicationUser> userManager
        , RoleManager<IdentityRole> roleManager
        , IOptions<IdentityOptions> optionsAccessor)
    : base(userManager, roleManager, optionsAccessor)
    { }

    public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
    {
        var principal = await base.CreateAsync(user);

        if (!string.IsNullOrWhiteSpace(user.FirstName))
        {
        ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
        new Claim(ClaimTypes.GivenName, user.FirstName)
    });
        }

        if (!string.IsNullOrWhiteSpace(user.LastName))
        {
            ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
         new Claim(ClaimTypes.Surname, user.LastName),
    });
        }

        return principal;
    }
}

将此行添加到我的Startup.cs文件中

Added this line to my Startup.cs file

services.AddScoped< Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory< ApplicationUser> ;, AppClaimsPrincipalFactory>();

这篇关于从身份扩展属性的问题(ASP.NET Core)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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