gpt4 book ai didi

c# - IIdentity、IPrincipal 或 IUser : what's the difference?

转载 作者:太空狗 更新时间:2023-10-29 19:40:52 25 4
gpt4 key购买 nike

我正在尝试在 MVC5 应用程序中实现简单的基于角色的身份验证 + 授权,但我在尝试理解身份框架中涉及的所有部分时感到有些头疼

我正在阅读一些教程和指南,但我仍然没有一个清晰的想法。

特别是:IIdentityIPrincipalIUser 接口(interface)之间的区别是什么?我应该实现其中的哪一个?

最佳答案

'IPrincipal' 是一个 .Net framework's interface :

public interface IPrincipal {
// Retrieve the identity object
IIdentity Identity { get; }

// Perform a check for a specific role
bool IsInRole (string role);
}

这个接口(interface)定义了登录用户的基本功能。实现此接口(interface)的对象表示您的代码在其下运行的安全上下文。您可以在 .Net 中获得不同风格的 IPrincipal:ClaimsPrincipalWindowsPrincipal 和其他 - 所有这些都取决于您使用的框架。如果您正在使用 Asp.Net Identity 框架,您将处理 ClaimsPrincipal。通常你不需要实现这个接口(interface)。

IIdentity 表示用户的权限。对于 Asp.Net Identity 框架,您将处理 ClaimsIdentity .同样,这是您不需要实现的事情。

Here is more documentation关于 IPrincipalIIDentity

IUser 是 Asp.Net Identity 框架的一部分。如果您使用身份的 Entity Framework 部分,you'll be provided with您可以继承和扩展的 IdentityUser 类。这是供您实现的模型。

基本上 IdentityUser 是一个保存在数据库中的 POCO。当用户登录时,来自IdentityUser 的信息将被框架转换为ClaimsPrincipalClaimsIdentity。当您访问 HttpContext.Current.User 时,您将获得 ClaimsPrincipal

希望这能为您解决问题。

关于c# - IIdentity、IPrincipal 或 IUser : what's the difference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477485/

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