Identity 2.0声明未添加到数据库 [英] Identity 2.0 Claims not being added to the database

查看:104
本文介绍了Identity 2.0声明未添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVC5与最新版本的Identity(2.1)结合使用,我正在尝试为facebook access_token创建用户声明.我以前从未提出过声明,但据我所知,我的其他身份功能正常运行.

I'm using MVC5 with the latest version of Identity (2.1) I'm trying to create a user claim for the facebook access_token. I've never created a claim before, but my other Identity functionality works fine as far as I can tell.

我的Startup.Auth.cs中有以下代码行:

I have this line of code in my Startup.Auth.cs:

    context.Identity.AddClaim(new Claim("urn:facebook:access_token",
 context.AccessToken, xmlSchemaString, "Facebook"));

如果需要更多参考,请参见完整代码:将facebooksdk与Identity 2.0集成

The full piece of code is here if you need more reference: Integrate facebooksdk with Identity 2.0

如果我在该行之后立即在行中插入代码,则可以看到所有内容都已正确检索,最重要的是content.AccessToken(这是一个巨大的字符串).但是,成功登录后,它再也不会进入数据库.

If I put a break in the code on the line immediately after that line, I can see that everything is being retrieved properly, most importantly the content.AccessToken (which is a huge string). However, it never makes it to the database after completing a successful login.

作为测试,我尝试通过将行更改为以下内容来简化它:

As a test, I tried simplifying it, by changing the line to this:

context.Identity.AddClaim(new System.Security.Claims.Claim(ClaimTypes.Email, "test@example.com"));

相同的结果,没有错误,但是没有任何内容添加到数据库中.然后,我尝试将这行代码添加到IdentityModels.cs中,该行告诉您放置自定义声明:

Same outcome, no errors, but nothing is added to the database. I then tried adding this line of code in my IdentityModels.cs right where it tells you to put custom claims:

// Add custom user claims here
        userIdentity.AddClaim(new Claim(ClaimTypes.DateOfBirth, "01/01/1972"));

相同的结果...没有错误,也永远不会进入数据库.谁能想到我的问题是什么原因?

Same outcome...no errors and never makes it to the database. Can anyone think of any reason what my issue might be?

身份"设置中的唯一自定义内容是,我遵循了一篇有关如何使用用户名而不是电子邮件(作为用户名)的文章.另外,我在OnModelCreating块中更改了身份表名称(例如UserClaims),这似乎是一个相当标准的过程.

The only thing custom in my Identity setup is that I followed an article on how to use username instead of email (as the username). Also, I changed the Identity table names (e.g. UserClaims) in the OnModelCreating block which seems to be a fairly standard procedure.

我感觉这将是一些新秀动作,但此刻,我很沮丧.非常感谢您的帮助.

I have a feeling it's going to be some rookie move, but at the moment, I'm stumped. Any help is much appreciated.

推荐答案

数据库会为用户保留您的自定义声明.如果用户在数据库中有任何声明,则他们将在登录时将其应用于身份验证cookie.

Database persists your custom claims for the users. If user has any claims in the DB, they will be applied to the auth cookie with they login.

要将声明添加到数据库中,您需要使用UserManager:

To add claims into the database you need to use UserManager:

await userManager.AddClaimAsync(userId, new Claim("MyClaimType", "MyClaimValue"));

如果将声明添加到ClaimsIdentity,则声明不会保留在数据库中,而是直接添加到cookie中,并且在用户下次登录时不会自动重新添加.

If you are adding claims to ClaimsIdentity, then claims are not persisted in the database, but added to a cookie directly and will not be automatically re-added next time the user is logged-in.

这篇关于Identity 2.0声明未添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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