Tag: class extensions

Identity 2.0:创建自定义ClaimsIdentity例如:User.Identity.GetUserById (int id)for Per Request Validation

请参阅此类似问题: 需要在User.Identity中访问更多用户属性 我想创建自定义身份validation方法以与我的Razor Views一起使用,这样可以轻松访问与User.Identity对象关联的IdentityUser属性,但我不知道如何去做。 我想创建几个类似于User.Identity.GetUserName() , User.Identity.GetUserById()等的自定义扩展…而不是使用此ViewContextExtension方法。 我的身份validation类型当前是VS2013 MVC5模板的默认类型DefaultAuthenticationTypes.ApplicationCookie 。 正如Shoe所说,我需要在用户登录后插入此声明。 我的问题是: 如何以及在何处创建一个在IPrincipal下具有this IIdentity的out参数的自定义声明? 这将允许我通过CookieAuthentication在DDD设置中的实体的ViewAuthentication中访问用户属性,其中我在使用Identity 2.0的单个应用程序中有多个DbContexts。 我最终将使用WebAPI,但是现在我希望尽可能简单。 我已经找到了这个SO Q&A但是它适用于使用Tickets的Web Forms。 不确定门票和代币之间的区别吗? 这是使用基本控制器中的ViewContext的当前方法: 视图: @using Microsoft.AspNet.Identity @using Globals.Helpers @using Identity //custom Identity for Domain @using Microsoft.AspNet.Identity.Owin @if (Request.IsAuthenticated) { var url = @ViewContext.BaseController().GetAvatarUrlById(User.Identity.GetUserId()); //… } BaseController.cs public string GetAvatarUrlById(int id) { var user = UserManager.FindById(id); return […]