c# - 如何扩展 Microsoft.AspNet.Identity.IUser<TKey>

标签 c# asp.net asp.net-identity

我正在尝试为我的 Web 应用程序实现一个带有电子邮件确认的注册过程。我正在使用 IdentityServer3.AspNetIdentity 服务。

public class AspNetIdentityUserService<TUser, TKey> : UserServiceBase
    where TUser : class, Microsoft.AspNet.Identity.IUser<TKey>, new()
    where TKey : IEquatable<TKey>
{

我的问题是 TUser 需要 Microsoft.AspNet.Identity.IUser 接口(interface)。在我实现的功能中,传入的用户上下文是正确的,但我无法访问您在以下两张图片中看到的所有属性。

enter image description here

enter image description here

http://img5.fotos-hochladen.net/uploads/b6aa42c662dd1kv063h4g5p.png http://img5.fotos-hochladen.net/uploads/d75b1e6462dd1wi9clqj6e7.png

您知道如何扩展 Microsoft.AspNet.Identity.IUser 以达到我有权访问属性 Lastname 和 Firstname 的效果吗?

最佳答案

这里的TUserIdentityServer3中自定义实现的类,它继承自IdentityUser,用于用户身份。

您无法访问这些属性,因为 TUser 是 IdentityServer3.Host.WebHost.AspId.User 的对象,由基类 IdentityUserIUser< 表示.

Quickwatch 会显示对象属性,但它不会出现在智能感知中。

您需要将 TUser 转换为 IdentityServer3.Host.WebHost.AspId.User

var user = new TUser() as IdentityServer3.Host.WebHost.AspId.User;
user.FirstName = "Tester";
user.LastName = "Doer";

这应该为您提供解决方法。

关于c# - 如何扩展 Microsoft.AspNet.Identity.IUser<TKey>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32783995/

相关文章:

c# - 为什么应该使用 ContainsKey 而不是 TryGetValue 是有原因的

c# - ASP.Net MVC Razor Sum 和 Count 函数

javascript - 在 ASP.NET 表单中提交更改的 FormData,以便通过 ModelState 和重定向实现正常行为

asp.net-core - ASP.NET Core 身份角色、声明和用户

asp.net-web-api - 身份验证和授权作为中央微服务 ASP.NET

c# - 如何在 "website"模式下使用 ResourceManager?

c# - 类型转换的性能

c# - 关于语法糖

ASP.NET MVC - Ajaxified RenderAction

c# - 在给定用户已经有 cookie 的情况下启动应用程序时处理 null HttpContext.Current