In the previous example we did an impersonation of a windows user on a service operation. With the PrincipalPermission attribute we can also provide role based security. To test this on the WriteToLogFile operation of the EchoService, we create a local group named EchoLogWriters. In this group the user WcfTester is placed.
On the service operation we declare the PrincipalPermission as follows.
[PrincipalPermission(SecurityAction.Demand, Role = "l040\\EchoLogWriters")] public void WriteToLogFile(string logText) { const string fileName = @"C:\logdir\logfile.txt"; StreamWriter streamWriter = null; try { streamWriter = File.Exists(fileName) ? File.AppendText(fileName) : new StreamWriter(fileName); streamWriter.WriteLine("{2} - {0}: {1}", Thread.CurrentPrincipal.Identity.Name, logText, DateTime.Now.ToLongTimeString()); } catch (Exception) { throw new FaultException("Log Access Denied"); } finally { if (streamWriter != null) streamWriter.Close(); } }
Now any user who is a member of the EchoLogWriters group can call the WriteToLogFile operation.









