-
Notifications
You must be signed in to change notification settings - Fork 12
/
CustomSecurityCheckAttribute.cs
113 lines (96 loc) · 3.91 KB
/
CustomSecurityCheckAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.Reflection;
using System.IdentityModel.Tokens;
using System.ServiceModel;
using WCFServer.DAL;
namespace WCFServer.Security.CustomSecurityCheck
{
/// <summary>
/// Invoker Security check behaviors attibute
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Interface)]
public class CustomSecurityCheckAttribute : Attribute, IOperationBehavior, IServiceBehavior
{
public enum CheckType
{
UserLoggedIn,
HasRole,
FromIP
}
public class CheckItem
{
public CheckType Type { get; set; }
public string value { get; set; }
}
public readonly List<CheckItem> CheckList = new List<CheckItem>();
public CustomSecurityCheckAttribute(CheckType type)
{
CheckList.Add(new CheckItem
{
Type = type
});
}
public CustomSecurityCheckAttribute(CheckType type, string value)
{
CheckList.Add(new CheckItem
{
Type = type,
value = value
});
}
public CustomSecurityCheckAttribute(List<CheckItem> checks)
{
CheckList.AddRange(checks);
}
#region IOperationBehavior Members
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
//Injecting an invoker to the IOperationBehavior, note the passing of the previous invoker
//so they are chained
dispatchOperation.Invoker = new CustomSecurityCheckInvoker(dispatchOperation.Invoker, this.CheckList.ToArray());
}
public void Validate(OperationDescription operationDescription)
{
}
#endregion
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
//Go over all endpoints of this service
foreach (var endpoint in serviceHostBase.Description.Endpoints)
{
//for each operation (eg. method)
foreach (var operation in endpoint.Contract.Operations)
{
//if an Invoker is already preset, merge Checklists, otherwise, create new
if (operation.Behaviors.Contains(typeof(CustomSecurityCheckAttribute)))
{
var customsecuritychecksbehavior = operation.Behaviors[typeof(CustomSecurityCheckAttribute)] as CustomSecurityCheckAttribute;
customsecuritychecksbehavior.CheckList.AddRange(this.CheckList);
}
else
{
operation.Behaviors.Add(new CustomSecurityCheckAttribute(CheckList));
}
}
}
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
#endregion
}
}