IIdentity、IPrincipal 或 IUser:有什么区别?
IIdentity, IPrincipal or IUser: what's the difference?
我正在尝试在 MVC5 应用程序中实现简单的基于角色的身份验证 + 授权,但我在尝试理解身份框架中涉及的所有部分时感到有些头疼
我正在阅读一些教程和指南,但我仍然没有一个清晰的想法。
特别是:IIdentity
、IPrincipal
或 IUser
接口之间的区别是什么?我应该实施其中的哪一个?
'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);
}
该接口定义了登录用户的基本功能。
实现此接口的对象表示您的代码所在的安全上下文 运行。您可以在 .Net 中获得不同风格的 IPrincipal
:ClaimsPrincipal
、WindowsPrincipal
和其他 - 这一切都取决于您使用的框架。如果您正在使用 Asp.Net 身份框架,那么您将要处理 ClaimsPrincipal
。
通常你不需要实现这个接口。
IIdentity
代表用户的权限。对于 Asp.Net 身份框架,您将处理 ClaimsIdentity。
同样,这是您不需要实施的事情。
Here is more documentation关于IPrincipal
和IIDentity
.
IUser
是 Asp.Net 身份框架的一部分。如果您正在使用身份的 Entity Framework 部分,您可以继承和扩展 you'll be provided with IdentityUser
class。这是您实施的模型。
基本上IdentityUser
是一个保存在数据库中的POCO。当用户登录时,来自 IdentityUser
的信息将被框架转换为 ClaimsPrincipal
和 ClaimsIdentity
。当您访问 HttpContext.Current.User
时,您将获得 ClaimsPrincipal
.
希望这能帮您解决问题。
我正在尝试在 MVC5 应用程序中实现简单的基于角色的身份验证 + 授权,但我在尝试理解身份框架中涉及的所有部分时感到有些头疼
我正在阅读一些教程和指南,但我仍然没有一个清晰的想法。
特别是:IIdentity
、IPrincipal
或 IUser
接口之间的区别是什么?我应该实施其中的哪一个?
'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);
}
该接口定义了登录用户的基本功能。
实现此接口的对象表示您的代码所在的安全上下文 运行。您可以在 .Net 中获得不同风格的 IPrincipal
:ClaimsPrincipal
、WindowsPrincipal
和其他 - 这一切都取决于您使用的框架。如果您正在使用 Asp.Net 身份框架,那么您将要处理 ClaimsPrincipal
。
通常你不需要实现这个接口。
IIdentity
代表用户的权限。对于 Asp.Net 身份框架,您将处理 ClaimsIdentity。
同样,这是您不需要实施的事情。
Here is more documentation关于IPrincipal
和IIDentity
.
IUser
是 Asp.Net 身份框架的一部分。如果您正在使用身份的 Entity Framework 部分,您可以继承和扩展 you'll be provided with IdentityUser
class。这是您实施的模型。
基本上IdentityUser
是一个保存在数据库中的POCO。当用户登录时,来自 IdentityUser
的信息将被框架转换为 ClaimsPrincipal
和 ClaimsIdentity
。当您访问 HttpContext.Current.User
时,您将获得 ClaimsPrincipal
.
希望这能帮您解决问题。