/
NetTcpSecurity.cs
84 lines (71 loc) · 2.82 KB
/
NetTcpSecurity.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.ServiceModel.Channels;
namespace System.ServiceModel
{
public sealed class NetTcpSecurity
{
internal const SecurityMode DefaultMode = SecurityMode.Transport;
private SecurityMode _mode;
public NetTcpSecurity() : this(DefaultMode, new TcpTransportSecurity(), new MessageSecurityOverTcp()) { }
private NetTcpSecurity(SecurityMode mode, TcpTransportSecurity transportSecurity, MessageSecurityOverTcp messageSecurity)
{
Contract.Assert(SecurityModeHelper.IsDefined(mode),
string.Format("Invalid SecurityMode value: {0} = {1} (default is {2} = {3}).",
(int)mode,
mode.ToString(),
(int)SecurityMode.Transport,
SecurityMode.Transport.ToString()));
_mode = mode;
Transport = transportSecurity ?? new TcpTransportSecurity();
Message = messageSecurity ?? new MessageSecurityOverTcp();
}
[DefaultValue(DefaultMode)]
public SecurityMode Mode
{
get { return _mode; }
set
{
if (!SecurityModeHelper.IsDefined(value))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value)));
}
_mode = value;
}
}
public TcpTransportSecurity Transport { get; set; }
public MessageSecurityOverTcp Message { get; set; }
internal BindingElement CreateTransportSecurity()
{
if (_mode == SecurityMode.TransportWithMessageCredential)
{
return Transport.CreateTransportProtectionOnly();
}
else if (_mode == SecurityMode.Transport)
{
return Transport.CreateTransportProtectionAndAuthentication();
}
else
{
return null;
}
}
internal SecurityBindingElement CreateMessageSecurity(bool isReliableSessionEnabled)
{
if (_mode == SecurityMode.Message)
{
throw new PlatformNotSupportedException();
}
else if (_mode == SecurityMode.TransportWithMessageCredential)
{
return Message.CreateSecurityBindingElement(true, isReliableSessionEnabled, CreateTransportSecurity());
}
else
{
return null;
}
}
}
}