惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
V
V2EX
WordPress大学
WordPress大学
博客园 - Franky
Last Week in AI
Last Week in AI
博客园 - 司徒正美
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
A
Arctic Wolf
量子位
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
雷峰网
雷峰网
博客园_首页
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
IT之家
IT之家
The Cloudflare Blog
爱范儿
爱范儿
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - N/A2011

Managing Hierarchical Data in MySQL php soapclient with wsse 转贴 MySQL Multiple Result Procs in PHP 转贴 jQuery Datepicker by Example 转贴 Using MySQL Stored Procedures with PHP mysql/mysqli/pdo php generate pdf open office (java) ant + emma + junit Copy all files recursively from one folder to another RecursiveFileFinder 转贴: 怎样找第一份工作 PerformanceCounter in .net Trace in .net Logger in .net 转贴: 傅立叶级数(Fourier Series) 推导 CAS in .net Encrypting and Decrypting in .net Access Control List in .net Unmanaged code in .net
User and Data Security in .net
N/A2011 · 2009-05-18 · via 博客园 - N/A2011

VB.NET:

Imports System.Security.Principal
Public Class JoeyIdentity
    
Implements IIdentityPublic ReadOnly Property AuthenticationType() As String Implements System.Security.Principal.IIdentity.AuthenticationType
        
Get
            
Return "Authenticated by Joey"
        
End Get
    
End PropertyPublic ReadOnly Property IsAuthenticated() As Boolean Implements System.Security.Principal.IIdentity.IsAuthenticated
        
Get
            
Return True
        
End Get
    
End PropertyPublic ReadOnly Property Name() As String Implements System.Security.Principal.IIdentity.Name
        
Get
            
Return "Joey"
        
End Get
    
End Property
End Class

Imports System.Security.Principal
Public Class JoeyPrincipal
    
Implements IPrincipal
    
Private _Identity As IIdentity
    
Private _Roles() As StringPublic Sub New(ByVal Identify As IIdentity, ByVal Roles As String())
        _Identity 
= Identify
        _Roles 
= Array.CreateInstance(GetType(String), Roles.Length)
        Roles.CopyTo(_Roles, 
0)
        Array.Sort(_Roles)
    
End SubPublic ReadOnly Property Identity() As System.Security.Principal.IIdentity Implements System.Security.Principal.IPrincipal.Identity
        
Get
            
Return _Identity
        
End Get
    
End PropertyPublic Function IsInRole(ByVal role As StringAs Boolean Implements System.Security.Principal.IPrincipal.IsInRole
        
Return Array.BinarySearch(_Roles, role) >= 0
    
End Function
End Class

Imports System.Threading
Imports System.Security.PermissionsModule Module1Sub Main()
        
Dim jIdentity As New JoeyIdentity
        
Dim roles As String() = {"Developer"}
        
Dim jPrincipal As New JoeyPrincipal(jIdentity, roles)
        Thread.CurrentPrincipal 
= jPrincipal
        
Try
            TestDeveloper()
            TestSucker()
        
Catch ex As Exception
            Console.WriteLine(ex.GetType.ToString 
+ " caused by " + Thread.CurrentPrincipal.Identity.Name)
        
End Try
        
Try
            
Dim pp As New PrincipalPermission("Joey""Developer")
            pp.Demand()
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is indeed a developer.")
            pp 
= New PrincipalPermission("Joey""Sucker")
            pp.Demand()
        
Catch ex As Exception
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is indeed not a sucker.")
        
End Try
        Console.Read()
    
End Sub<PrincipalPermissionAttribute(SecurityAction.Demand, role:="Developer")> _
    
Private Sub TestDeveloper()
        Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is a developer.")
    
End Sub<PrincipalPermissionAttribute(SecurityAction.Demand, role:="Sucker")> _
   
Private Sub TestSucker()
        Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is a sucker.")
    
End SubEnd Module

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;namespace UserAndDataSecurityCS
{
    
class JoeyIdentity : IIdentity
    {
        
#region IIdentity Memberspublic string AuthenticationType
        {
            
get
            {
                
return "Authenticated by Joey";
            }
        }
public bool IsAuthenticated
        {
            
get
            {
                
return true;
            }
        }
public string Name
        {
            
get
            {
                
return "Joey";
            }
        }
#endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;namespace UserAndDataSecurityCS
{
    
class JoeyPrincipal : IPrincipal
    {
        
private IIdentity _Identity;
        
private string[] _Roles;public JoeyPrincipal(IIdentity Identity, string[] Roles)
        {
            _Identity 
= Identity;
            _Roles 
= new string[Roles.Length];
            Roles.CopyTo(_Roles, 
0);
            Array.Sort(_Roles);
        }
#region IPrincipal Memberspublic IIdentity Identity
        {
            
get
            {
                
return _Identity;
            }
        }
public bool IsInRole(string role)
        {
            
return Array.BinarySearch<string>(_Roles, role) >= 0;
        }
#endregion
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Threading;namespace UserAndDataSecurityCS
{
    
class Program
    {
        
static void Main(string[] args)
        {
            JoeyIdentity jIdentify 
= new JoeyIdentity();
            
string[] roles = { "Developer" };
            JoeyPrincipal jPrincipal 
= new JoeyPrincipal(jIdentify, roles);
            Thread.CurrentPrincipal 
= jPrincipal;
            
try
            {
                TestDeveloper();
                TestSucker();
            }
            
catch(Exception ex)
            {
                Console.WriteLine(ex.GetType().ToString() 
+ " caused by " + Thread.CurrentPrincipal.Identity.Name);
            }
            
try
            {
                PrincipalPermission pp 
= new PrincipalPermission("Joey""Developer");
                pp.Demand();
                Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is indeed a developer.");
                pp 
= new PrincipalPermission("Joey""Sucker");
                pp.Demand();
            }
            
catch
            {
                Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is indeed not a sucker.");
            }
            Console.Read();
        }

        [PrincipalPermissionAttribute(SecurityAction.Demand, Role 

= "Developer")]
        
private static void TestDeveloper()
        {
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is a developer.");
        }

        [PrincipalPermissionAttribute(SecurityAction.Demand, Role 

= "Sucker")]
        
private static void TestSucker()
        {
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name 
+ " is a sucker.");
        }
    }
}