博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]How to: Create a Custom Principal Identity
阅读量:5825 次
发布时间:2019-06-18

本文共 3226 字,大约阅读时间需要 10 分钟。

本文转自:

The is a declarative means of controlling access to service methods. When using this attribute, the enumeration specifies the mode for performing authorization checks. When this mode is set to , it enables the user to specify a custom class returned by the property. This topic illustrates the scenario when is used in combination with a custom authorization policy and a custom principal.

For more information about using the , see .

Example

C#
namespace CustomMode{    public class Test    {        public static void Main() { try { ShowPrincipalPermissionModeCustom ppwm = new ShowPrincipalPermissionModeCustom(); ppwm.Run(); } catch (Exception exc) { Console.WriteLine("Error: {0}", exc.Message); Console.ReadLine(); } } } class ShowPrincipalPermissionModeCustom { [ServiceContract] interface ISecureService { [OperationContract] string Method1(string request); } [ServiceBehavior] class SecureService : ISecureService { [PrincipalPermission(SecurityAction.Demand, Role = "everyone")] public string Method1(string request) { return String.Format("Hello, \"{0}\"", Thread.CurrentPrincipal.Identity.Name); } } public void Run() { Uri serviceUri = new Uri(@"http://localhost:8006/Service"); ServiceHost service = new ServiceHost(typeof(SecureService)); service.AddServiceEndpoint(typeof(ISecureService), GetBinding(), serviceUri); List
policies = new List
(); policies.Add(new CustomAuthorizationPolicy()); service.Authorization.ExternalAuthorizationPolicies = policies.AsReadOnly(); service.Authorization.PrincipalPermissionMode = PrincipalPermissionMode.Custom; service.Open(); EndpointAddress sr = new EndpointAddress( serviceUri, EndpointIdentity.CreateUpnIdentity(WindowsIdentity.GetCurrent().Name)); ChannelFactory
cf = new ChannelFactory
(GetBinding(), sr); ISecureService client = cf.CreateChannel(); Console.WriteLine("Client received response from Method1: {0}", client.Method1("hello")); ((IChannel)client).Close(); Console.ReadLine(); service.Close(); } public static Binding GetBinding() { WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message); binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows; return binding; } class CustomAuthorizationPolicy : IAuthorizationPolicy { string id = Guid.NewGuid().ToString(); public string Id { get { return this.id; } } public ClaimSet Issuer { get { return ClaimSet.System; } } public bool Evaluate(EvaluationContext context, ref object state) { object obj; if (!context.Properties.TryGetValue("Identities", out obj)) return false; IList
identities = obj as IList
; if (obj == null || identities.Count <= 0) return false; context.Properties["Principal"] = new CustomPrincipal(identities[0]); return true; } } class CustomPrincipal : IPrincipal { IIdentity identity; public CustomPrincipal(IIdentity identity) { this.identity = identity; } public IIdentity Identity { get { return this.identity; } } public bool IsInRole(string role) { return true; } } } }

Compiling the Code

References to the following namespaces are needed to compile the code:

 

转载地址:http://hhidx.baihongyu.com/

你可能感兴趣的文章
nvl 在mysql中如何处理
查看>>
MyEclipse 快捷键
查看>>
快速傅里叶变换FFT
查看>>
大数据常用基本算法
查看>>
JavaScript学习笔记(十三)——生成器(generator)
查看>>
hibernate保存失败
查看>>
MySQL增量订阅&消费组件Canal POC
查看>>
Sqlite多线程
查看>>
数据结构-时间复杂度
查看>>
对象与字符串相互转换
查看>>
[NOIp2017提高组]小凯的疑惑
查看>>
《C程序设计语言》练习1-5
查看>>
$\frac{dy}{dx}$ 是什么意思?
查看>>
Go开发之路(目录)
查看>>
RHEL6.5安装成功ORACLE11GR2之后,编写PROC程序出错解决方法
查看>>
(50)与magento集成
查看>>
Ubuntu设置python3为默认版本
查看>>
JsonCpp 的使用
查看>>
问题账户需求分析
查看>>
JavaSE-代码块
查看>>