2 // Copyright (c) Microsoft Corporation. All Rights Reserved.
5 using System
.IdentityModel
.Tokens
;
6 using System
.ServiceModel
;
7 using System
.Security
.Principal
;
8 using System
.Threading
;
9 using System
.Web
.Security
;
10 using System
.ServiceModel
.Security
;
11 using System
.IdentityModel
.Selectors
;
13 namespace Microsoft
.ServiceModel
.Samples
15 // Define a service contract.
16 [ServiceContract(Namespace
= "http://Microsoft.ServiceModel.Samples")]
17 public interface ICalculator
20 double Add(double n1
, double n2
);
22 double Subtract(double n1
, double n2
);
24 double Multiply(double n1
, double n2
);
26 double Divide(double n1
, double n2
);
29 // Service class which implements the service contract.
30 // Added code to write output to the console window
31 public class CalculatorService
: ICalculator
33 static void DisplayIdentityInformation()
35 Console
.WriteLine("\t\tSecurity context identity : {0}", ServiceSecurityContext
.Current
.PrimaryIdentity
.Name
);
38 public double Add(double n1
, double n2
)
40 DisplayIdentityInformation();
41 double result
= n1
+ n2
;
42 Console
.WriteLine("Received Add({0},{1})", n1
, n2
);
43 Console
.WriteLine("Return: {0}", result
);
47 public double Subtract(double n1
, double n2
)
49 DisplayIdentityInformation();
50 double result
= n1
- n2
;
51 Console
.WriteLine("Received Subtract({0},{1})", n1
, n2
);
52 Console
.WriteLine("Return: {0}", result
);
56 public double Multiply(double n1
, double n2
)
58 DisplayIdentityInformation();
59 double result
= n1
* n2
;
60 Console
.WriteLine("Received Multiply({0},{1})", n1
, n2
);
61 Console
.WriteLine("Return: {0}", result
);
65 public double Divide(double n1
, double n2
)
67 DisplayIdentityInformation();
68 double result
= n1
/ n2
;
69 Console
.WriteLine("Received Divide({0},{1})", n1
, n2
);
70 Console
.WriteLine("Return: {0}", result
);
75 // Host the service within this EXE console application.
76 public static void Main()
78 // Create a ServiceHost for the CalculatorService type and provide the base address.
79 using (ServiceHost serviceHost
= new ServiceHost(typeof(CalculatorService
)))
81 serviceHost
.Credentials
.UserNameAuthentication
.UserNamePasswordValidationMode
= UserNamePasswordValidationMode
.Custom
;
82 serviceHost
.Credentials
.UserNameAuthentication
.CustomUserNamePasswordValidator
= new MyUserNamePasswordValidator();
84 // Open the ServiceHostBase to create listeners and start listening for messages.
87 // The service can now be accessed.
88 Console
.WriteLine("The service is ready.");
89 Console
.WriteLine("The service is running in the following account: {0}", WindowsIdentity
.GetCurrent().Name
);
90 Console
.WriteLine("Press <ENTER> to terminate service.");
97 public class MyUserNamePasswordValidator
: UserNamePasswordValidator
99 public override void Validate(string userName
, string password
)
101 // check that username matches password
102 if (null == userName
|| userName
!= password
)
104 Console
.WriteLine("Invalid username or password");
105 throw new SecurityTokenValidationException("Invalid username or password");