4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . Configuration ;
7
-
8
7
using System . IdentityModel . Claims ;
9
8
using System . IdentityModel . Policy ;
10
- using System . IdentityModel . Tokens ;
11
9
using System . IdentityModel . Selectors ;
12
-
13
10
using System . Security . Principal ;
14
11
15
12
using System . ServiceModel ;
16
- using System . ServiceModel . Description ;
17
- using System . ServiceModel . Security ;
18
13
19
14
namespace Microsoft . ServiceModel . Samples
20
15
{
21
16
// Define a service contract.
22
- [ ServiceContract ( Namespace = "http://microsoft.servicemodel.samples/" ) ]
17
+ [ ServiceContract ( Namespace = "http://microsoft.servicemodel.samples/" ) ]
23
18
public interface ICalculator
24
19
{
25
20
[ OperationContract ]
@@ -34,16 +29,16 @@ public interface ICalculator
34
29
35
30
// Service class that implements the service contract.
36
31
// Added code to write output to the console window.
37
- [ ServiceBehavior ( IncludeExceptionDetailInFaults = true ) ]
38
-
32
+ [ ServiceBehavior ( IncludeExceptionDetailInFaults = true ) ]
33
+
39
34
public class CalculatorService : ICalculator
40
35
{
41
36
public double Add ( double n1 , double n2 )
42
37
{
43
38
double result = n1 + n2 ;
44
39
Console . WriteLine ( "Received Add({0},{1})" , n1 , n2 ) ;
45
40
Console . WriteLine ( "Return: {0}" , result ) ;
46
- return result ;
41
+ return result ;
47
42
}
48
43
49
44
public double Subtract ( double n1 , double n2 )
@@ -74,7 +69,7 @@ public double Divide(double n1, double n2)
74
69
public class MyServiceAuthorizationManager : ServiceAuthorizationManager
75
70
{
76
71
77
- protected override bool CheckAccessCore ( OperationContext operationContext )
72
+ protected override bool CheckAccessCore ( OperationContext operationContext )
78
73
{
79
74
// Extract the action URI from the OperationContext. Match this against the claims
80
75
// in the AuthorizationContext.
@@ -83,7 +78,7 @@ protected override bool CheckAccessCore(OperationContext operationContext)
83
78
Console . WriteLine ( "action: {0}" , action ) ;
84
79
85
80
// Iterate through the various claim sets in the AuthorizationContext.
86
- foreach ( ClaimSet cs in operationContext . ServiceSecurityContext . AuthorizationContext . ClaimSets )
81
+ foreach ( ClaimSet cs in operationContext . ServiceSecurityContext . AuthorizationContext . ClaimSets )
87
82
{
88
83
// Examine only those claim sets issued by System.
89
84
if ( cs . Issuer == ClaimSet . System )
@@ -109,13 +104,14 @@ protected override bool CheckAccessCore(OperationContext operationContext)
109
104
}
110
105
}
111
106
// </snippet1>
107
+
112
108
public class MyAuthorizationPolicy : IAuthorizationPolicy
113
109
{
114
110
string id ;
115
111
116
112
public MyAuthorizationPolicy ( )
117
113
{
118
- id = Guid . NewGuid ( ) . ToString ( ) ;
114
+ id = Guid . NewGuid ( ) . ToString ( ) ;
119
115
}
120
116
121
117
//
@@ -158,7 +154,7 @@ public bool Evaluate(EvaluationContext evaluationContext, ref object state)
158
154
}
159
155
160
156
// Add claims to the evaluation context.
161
- evaluationContext . AddClaimSet ( this , new DefaultClaimSet ( this . Issuer , claims ) ) ;
157
+ evaluationContext . AddClaimSet ( this , new DefaultClaimSet ( this . Issuer , claims ) ) ;
162
158
163
159
// Record that the claims have been added.
164
160
customstate . ClaimsAdded = true ;
@@ -193,14 +189,14 @@ private IEnumerable<string> GetAllowedOpList(string username)
193
189
194
190
if ( username == "test1" )
195
191
{
196
- ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Add" ) ;
197
- ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Multiply" ) ;
192
+ ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Add" ) ;
193
+ ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Multiply" ) ;
198
194
ret . Add ( "http://Microsoft.ServiceModel.Samples/ICalculator/Subtract" ) ;
199
195
}
200
196
else if ( username == "test2" )
201
197
{
202
- ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Add" ) ;
203
- ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Subtract" ) ;
198
+ ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Add" ) ;
199
+ ret . Add ( "http://microsoft.servicemodel.samples/ICalculator/Subtract" ) ;
204
200
}
205
201
return ret ;
206
202
}
@@ -215,46 +211,37 @@ public CustomAuthState()
215
211
bClaimsAdded = false ;
216
212
}
217
213
218
- public bool ClaimsAdded { get { return bClaimsAdded ; }
219
- set { bClaimsAdded = value ; } }
214
+ public bool ClaimsAdded
215
+ {
216
+ get { return bClaimsAdded ; }
217
+ set { bClaimsAdded = value ; }
218
+ }
220
219
}
221
220
}
222
221
223
222
public class MyCustomUserNameValidator : UserNamePasswordValidator
224
223
{
225
- // This method validates users. It allows two users, test1 and test2
226
- // with passwords 1tset and 2tset respectively.
227
- // This code is for illustration purposes only and
228
- // MUST NOT be used in a production environment because it is NOT secure.
229
224
public override void Validate ( string userName , string password )
230
225
{
231
- if ( null == userName || null == password )
232
- {
233
- throw new ArgumentNullException ( ) ;
234
- }
235
-
236
- if ( ! ( userName == "test1" && password == "1tset" ) && ! ( userName == "test2" && password == "2tset" ) )
237
- {
238
- throw new SecurityTokenException ( "Unknown Username or Password" ) ;
239
- }
226
+ throw new NotImplementedException ( ) ;
240
227
}
241
228
}
242
229
243
230
// Host the service within this EXE console application.
244
231
public static void Main ( )
245
232
{
246
233
// Get the base address from appsettings in configuration.
247
- Uri baseAddress = new Uri ( ConfigurationManager . AppSettings [ "baseAddress" ] ) ;
234
+ Uri baseAddress = new ( ConfigurationManager . AppSettings [ "baseAddress" ] ) ;
248
235
249
236
// Create a ServiceHost for the CalculatorService type and provide the base address.
250
237
using ( ServiceHost serviceHost = new ServiceHost ( typeof ( CalculatorService ) , baseAddress ) )
251
238
{
252
239
// Open the ServiceHostBase to create listeners and start listening for messages.
253
240
serviceHost . Open ( ) ;
254
-
241
+
255
242
// The service can now be accessed.
256
243
Console . WriteLine ( "The service is ready." ) ;
257
- Console . WriteLine ( "The service is running in the following account: {0}" , WindowsIdentity . GetCurrent ( ) . Name ) ;
244
+ Console . WriteLine ( "The service is running in the following account: {0}" , WindowsIdentity . GetCurrent ( ) . Name ) ;
258
245
Console . WriteLine ( "Press <ENTER> to terminate service." ) ;
259
246
Console . WriteLine ( ) ;
260
247
Console . ReadLine ( ) ;
0 commit comments