我正在尝试在 MVC5 应用程序中实现简单的基于角色的身份验证 + 授权,但我在尝试理解身份框架中涉及的所有部分时感到有些头疼
我正在阅读一些教程和指南,但我仍然没有一个清晰的想法。
特别是:IIdentity
、IPrincipal
或 IUser
接口(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
:ClaimsPrincipal
、WindowsPrincipal
和其他 - 所有这些都取决于您使用的框架。如果您正在使用 Asp.Net Identity 框架,您将处理 ClaimsPrincipal
。
通常你不需要实现这个接口(interface)。
IIdentity
表示用户的权限。对于 Asp.Net Identity 框架,您将处理 ClaimsIdentity .
同样,这是您不需要实现的事情。
Here is more documentation关于 IPrincipal
和 IIDentity
。
IUser
是 Asp.Net Identity 框架的一部分。如果您使用身份的 Entity Framework 部分,you'll be provided with您可以继承和扩展的 IdentityUser
类。这是供您实现的模型。
基本上 IdentityUser
是一个保存在数据库中的 POCO。当用户登录时,来自IdentityUser
的信息将被框架转换为ClaimsPrincipal
和ClaimsIdentity
。当您访问 HttpContext.Current.User
时,您将获得 ClaimsPrincipal
。
希望这能为您解决问题。
关于c# - IIdentity、IPrincipal 或 IUser : what's the difference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477485/