[轉]How to: Create a Custom Principal Identity

來源:互聯網
上載者:User

標籤:oar   more   bind   names   eth   black   factor   dir   uid   

本文轉自: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.

ExampleC#VBCopy
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; } } }}
Compiling the Code

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

  • System

  • System.Collections.Generic

  • System.Security.Permissions

  • System.Security.Principal

  • System.Threading

  • System.ServiceModel

  • System.ServiceModel.Channels

  • System.ServiceModel.Description

  • System.IdentityModel.Claims

  • System.IdentityModel.Policy

 

[轉]How to: Create a Custom Principal Identity

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.