【问题标题】:'.../nameidentifier' was not present on the provided ClaimsIdentity提供的 ClaimsIdentity 上不存在“.../nameidentifier”
【发布时间】:2017-05-07 16:29:18
【问题描述】:

我使用 Identityserver3 作为 MVC 应用程序的授权服务器。因此,我的Startup 类是这样的:

public void Configuration(IAppBuilder app)
{

    app.UseExternalSignInCookie();
    JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = "Cookies"
    });
    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {
        Authority = "http://localhost:5000/",
        ClientId = "mvc",
        RedirectUri = "http://localhost:12262/",
        ResponseType = "id_token",
        UseTokenLifetime = false,
        SignInAsAuthenticationType = "Cookies"
    });
}

这是我的IdentityUser 子类:

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

        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        userIdentity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", this.Email));
        userIdentity.AddClaim(
            new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider",
                this.Email));

        return userIdentity;
    }

    public UserType UserType { get; set; }
    ....
}

这是我的Global.asax.cs

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}

但在我的Account/Register 视图中包含@Html.AntiForgeryToken() 的行中,我收到此错误:

类型的声明

'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' 提供的 ClaimsIdentity 中不存在。

我在 SO 上看到了一些类似问题的问题(可能没有在任何地方使用 Identityserver3),但他们的解决方案似乎不起作用,至少我使用它们的方式是这样。

【问题讨论】:

    标签: c# asp.net-mvc identityserver3


    【解决方案1】:

    恕我直言,我认为问题在于未在正确的位置添加声明。但要确认这一点,我需要一些反馈。您是否将 Microsoft.AspNet.Identity*(NuGet 包)与 Identity Server 结合使用? 你是通过代码创建用户还是从数据库中读取?

    例如,我将 AspNet.Identity 与 IdentityServer3 一起使用,并为每个用户添加声明,您可以修改用户服务 GetClaimsFromAccount 方法,如下所示:

    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Threading.Tasks;
    using IdentityServer3.Core;
    using IdentityServer3.AspNetIdentity;
    using IdentityServer3.Core.Configuration;
    using IdentityServer3.Core.Services;
    
    namespace ..... {
    
    public class UserService : AspNetIdentityUserService<IdentityUser, string>
    {
        public UserService(UserManager userMgr) : base(userMgr)
        {
        }
    
        protected override async Task<IEnumerable<System.Security.Claims.Claim>> GetClaimsFromAccount(IdentityUser user)
        {
            var claims = (await base.GetClaimsFromAccount(user)).ToList();
    
            // to make sure the email is in the claims
            if (claims.Any(c=>c.Type == Constants.ClaimTypes.Email) && !string.IsNullOrWhiteSpace(user.Email))
            {
                claims.Add(.....);
            }
    
            return claims;
        }
    }
    ....
    }
    

    请注意,identityServer3.core 带有包含声明类型 (Constants.Claimtypes.Email) 的常量。

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 2012-12-14
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 2017-01-26
      • 2020-02-05
      • 2017-10-01
      相关资源
      最近更新 更多