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

标签 c# .net asp.net-mvc-5 asp.net-identity

我正在尝试在 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/

相关文章:

c# - 在向 C# Controller 操作发出 POST 请求之前会发生什么

c# - 使用 new 关键字调用基本方法

c# - Excel中PrintArea的最大字符串长度

c# - 如何在大图像中定位图像部分的坐标?

asp.net - 在 Visual Studio 解决方案中创建实际的物理文件夹

c# - 如何在不影响其子项的情况下将属性应用于控件

.net - 哪些技术可用于使用 .NET 的发布/订阅模型?

c# - Entity Framework linq orderby 函数

c# - 全局 Asax 与全局过滤器

c# - 如何从全局过滤器的 NHibernate session 中获取更新的对象