简体   繁体   English

在AspNet OpenAuth登录MVC之后获取User.Identity.Name

[英]Getting User.Identity.Name after AspNet OpenAuth Login MVC

I am using OAuth to sing the user up with Facebook. 我正在使用OAuth通过Facebook来吸引用户。

I am doing successful signup using the following method:- 我正在使用以下方法成功注册:-

Request.GetOwinContext().Authentication.SignIn(id);

This Autheticates the user. 这会授权用户。 I can check this using User.Identity.IsAuthenticated 我可以使用User.Identity.IsAuthenticated进行检查

But I am not able to get the name of the User or any of his details using User.Identity.Name . 但是我无法使用User.Identity.Name获取用户的名称或任何详细信息。

How can I get the name of the User who has just been authenticated without going back to the database? 如何获得刚通过身份验证的用户的名称,而无需返回数据库?

The reason you can't get anything from User.Identity.Name is because there's no actual user record to pull that info from, which means hitting the database wouldn't help you anyways. 您无法从User.Identity.Name获得任何信息的原因是,因为没有实际的用户记录可从中提取该信息,这意味着访问数据库仍然无济于事。 Sign-in provider logins are handled separately from system user logins, in a separate table in fact. 实际上,登录提供程序登录名与系统用户登录名分开处理,位于单独的表中。 They can and often are tied to an actual user account, but they don't have to be. 它们可以而且经常与实际的用户帐户绑定,但是不必一定要绑定。 The provider login is enough to authenticate, and sometimes that may be all that's needed. 提供者登录足以验证身份,有时可能只需要这些即可。 If you do actually need the user information, then you'll need to create a user record after successful provider login and associate that with the provider login. 如果确实需要用户信息,则需要在成功的提供者登录后创建用户记录,并将其与提供者登录相关联。

The best response that thing I came up with is adding claims and adding the Name claim to the answer. 我想到的最好的回答是添加声明,并在答案中添加Name声明。

var newClaims = new Claim[]
                        {
                            new Claim(ClaimTypes.NameIdentifier, PersonId.ToString()),
                            new Claim(ClaimTypes.GivenName, FirstName),
                            new Claim(ClaimTypes.Surname, LastName),
                            new Claim(ClaimTypes.Name, FirstName + LastName),
                            new Claim(ClaimTypes.Email, Email)
                        };

The Name claim that I added would fill the User.Identity.Name field. 我添加的名称声明将填充User.Identity.Name字段。

I hope this helps someone. 我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM