[转]How to: Create a Custom Principal Identity

本文转自:https://msdn.microsoft.com/en-us/library/aa702720(v=vs.110).aspx

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

For more information about using the PrincipalPermissionAttribute, see How to: Restrict Access with the PrincipalPermissionAttribute Class.

Example

C#
VB
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<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(); 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<ISecureService> cf = new ChannelFactory<ISecureService>(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<IIdentity> identities = obj as IList<IIdentity>; 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; } } } } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值