您的位置:首页 > 其它

安全性-身份验证和授权(一)之Principal

2017-02-27 16:58 232 查看

1.概述

为了确保应用程序的安全,安全性有几个重要方面需要考虑。一是应用程序的用户,访问应用程序的是一个真正的用户,还是伪装成用户的某个人?如何确定这个用户是可以信任的?

确保应用程序安全的用户方面是一个2个阶段过程:

用户首先需要身份验证

再进行授权,已验证该用户是否可以使用需要的资源

对于在网络上存储或发送的数据呢?例如,有人可以通过网络嗅探器访问这些数据吗?这里数据加密很重要。一些技术,如WCF,通过简单的配置提供了加密功能,所以可以看到后台执行了什么操作。

另一方面是应用程序本身。如果应用程序驻留在WEB提供程序上,如何禁止应用程序执行对服务器有伤害的操作?

2.身份验证和授权

安全性的两个基本支柱是身份验证和授权。身份验证是标识用户的过程,授权在验证了所标识用户是否可以访问特定资源之后进行。

2.1标识和Principal

使用标识可以验证运行应该程序的用户。Windows Indentity 类表示一个Windows用户。如果没有用windows账户标识用户,也可以使用实现了Identity接口的其他类。通过这个接口可以访问用户名,该用户是否通过身份验证,以及验证类型等信息。

Principal是一个包含用户的标识和用户的所属角色的对象。IPrincipal接口定义了Identity属性和IsInRole方法,Identity属性返回Identity对象;在IsInRole方法中,可以验证用户是否是指定角色的一个成员。角色是有相同安全权限的用户集合,同时它是用户的管理单元。角色可以是Windows组或自己定义的一个字符串集合。

表示 Windows 用户。

命名空间: System.Security.Principal

程序集: mscorlib(位于 mscorlib.dll)

继承层次结构

System.Object

  System.Security.Claims.ClaimsIdentity

    System.Security.Principal.WindowsIdentity

语法

[SerializableAttribute]

[ComVisibleAttribute(true)]

public class WindowsIdentity : ClaimsIdentity, ISerializable,

IDeserializationCallback, IDisposable

构造函数

名称说明
WindowsIdentity(IntPtr)为指定的 Windows 帐户标记表示的用户初始化 WindowsIdentity 类的新实例。
WindowsIdentity(IntPtr, String)为指定的 Windows 帐户标记和指定的身份验证类型表示的用户初始化 WindowsIdentity 类的新实例。
WindowsIdentity(IntPtr, String, WindowsAccountType)为指定的 Windows 帐户标记、指定的身份验证类型和指定的 Windows 帐户类型表示的用户初始化 WindowsIdentity 类的新实例。
WindowsIdentity(IntPtr, String, WindowsAccountType, Boolean)为指定的 Windows 帐户标记、指定的身份验证类型、指定的 Windows 帐户类型和指定的身份验证状态表示的用户初始化 WindowsIdentity 类的新实例。
WindowsIdentity(SerializationInfo, StreamingContext)初始化 SerializationInfo 流中的信息所表示的用户的 WindowsIdentity 类的新实例。
WindowsIdentity(String)初始化以指定用户主名称 (UPN) 表示的用户的 WindowsIdentity 类的新实例。
WindowsIdentity(String, String)初始化以指定用户主名称 (UPN) 和指定身份验证类型共同表示的用户的 WindowsIdentity 类的新实例。
WindowsIdentity(WindowsIdentity)使用指定的 WindowsIdentity 对象初始化 WindowsIdentity 类的新实例。

属性





方法





字段



显式接口实现



protected void Page_Load(object sender, EventArgs e)
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

var principal = WindowsPrincipal.Current as WindowsPrincipal;

var identity = principal.Identity as WindowsIdentity;
Console.WriteLine("IdentityType:{0}",identity.ToString());
Console.WriteLine("Name:{0}", identity.Name);
Console.WriteLine("'user'?:{0}", principal.IsInRole(WindowsBuiltInRole.User));

Console.WriteLine("'administrtors'
4000
?:{0}", principal.IsInRole(WindowsBuiltInRole.Administrator));

Console.WriteLine("'authenticated'?:{0}",identity.IsAuthenticated);

Console.WriteLine("'authtype'?:{0}", identity.AuthenticationType);

Console.WriteLine("'anonymoues'?:{0}", identity.IsAnonymous);

Console.WriteLine("'token'?:{0}", identity.Token);

}




很显然用户可以很容易访问当前用户及其角色的详细信息,然后使用那些信息决定允许或拒绝用户执行某些动作,这就非常有好处。利用角色和windows用户组,管理员可以完成标准用户管理工具所能完成的工作,这样在用户的角色改变时,通常可以避免代码的更改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息