gpt4 book ai didi

c# - Identity 2.0 : Creating custom ClaimsIdentity eg: User. Identity.GetUserById(int id) 用于每个请求验证

转载 作者:太空狗 更新时间:2023-10-30 00:04:49 25 4
gpt4 key购买 nike

查看类似的问题:Need access more user properties in User.Identity

我想创建自定义身份验证方法以与我的 Razor View 一起使用,以便轻松访问与 User.Identity 对象相关的 IdentityUser 属性,但我不确定如何去做。我想创建几个类似于 User.Identity.GetUserName()User.Identity.GetUserById() 等的自定义扩展...而不是使用此 ViewContextExtension方法。我的身份验证类型当前是来自 VS2013 MVC5 模板的默认类型 DefaultAuthenticationTypes.ApplicationCookie。正如 Shoe 所说,我需要在用户登录后插入此声明。

我的问题是:

您如何以及在何处创建具有 IPrincipal 下 this IIdentity 输出参数的自定义声明?

这将允许我在 DDD 设置中的实体 View 中通过 CookieAuthentication 访问用户属性,其中我在使用 Identity 2.0 的单个应用程序中有多个 DbContext。我最终会使用 WebAPI,但现在我希望它尽可能简单。我找到了 this SO Q&A但它适用于使用票证的 Web 表单。也不确定门票和代币之间的区别?

这是当前使用来自基本 Controller 的 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<int>());

//...
}

BaseController.cs

        public string GetAvatarUrlById(int id)
{

var user = UserManager.FindById(id);

return "../../" + user.ImageUrl;
}

Extensions.cs

    public static class ViewContextExtension
{
public static BaseController BaseController(this ViewContext view)
{
var baseController = (BaseController)view.Controller;
return baseController;
}
}

我正在寻找的是在哪里以及如何寻找?

查看:

<img src="@User.Identity.GetAvatarUrl()" alt="User.Identity.GetAvatarUrl()" />

解决方案

我简单地编辑了 Extension.cs 文件并对用于 _LoginPartial.cshtml 的 Base Controller 使用继承并编辑了 ViewContextExtension 类:

    #region ViewContextExt
public static class ViewContextExtension
{
public static BaseController BaseController(this ViewContext view)
{
var baseController = (BaseController)view.Controller;
return baseController;
}

public static string GetAvatarUrl(this IIdentity identity)
{
return ((ClaimsIdentity)identity).Claims.First(c => c.Type == "AvatarUrl").Value;
}
}
}
# endregion

最佳答案

MVC 中的 IIdentity 对象将成为与用户身份相对应的颁发 token 。这不同于您在代表用户的后端使用的任何对象或方法(比如 User 类)。如果您想使用用户的身份来获取自定义值,那么您需要在他们登录时(或在其他某个时间点)将其放入他们的声明对象(即身份 token )中。

您可以随时通过为用户提供身份来添加声明。

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim("PhoneNumber", "123-456-7890"));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);

当您将该声明插入到他们的 token 中时,您可以使用像这样的扩展方法检索它...

public static string GetPhoneNumber(this IIdentity identity)
{
return ((ClaimsIdentity)identity).FindFirstValue("PhoneNumber");
}

Razor

@using MyProject.Web.Extensions

<img src="@User.Identity.GetPhoneNumber()" />

关于c# - Identity 2.0 : Creating custom ClaimsIdentity eg: User. Identity.GetUserById<int>(int id) 用于每个请求验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com