Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Extensibility/Security/AuthorizationPolicy/Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Using Include="CoreWcf.Samples.AuthorizationPolicy" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ServiceModel.Http" Version="4.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ExtendedData": {
"inputs": [
"https://localhost:5001/CalculatorService?wsdl"
],
"collectionTypes": [
"System.Array",
"System.Collections.Generic.Dictionary`2"
],
"namespaceMappings": [
"*, CoreWcf.Samples.AuthorizationPolicy"
],
"sync": true,
"targetFramework": "net6.0",
"typeReuseMode": "All"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CoreWcf.Samples.AuthorizationPolicy
{


[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://CoreWcf.Samples.AuthorizationPolicy", ConfigurationName="CoreWcf.Samples.AuthorizationPolicy.ICalculatorService")]
public interface ICalculatorService
{

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Add", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/AddResponse")]
double Add(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Add", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/AddResponse")]
System.Threading.Tasks.Task<double> AddAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Subtract", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/SubtractResponse")]
double Subtract(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Subtract", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/SubtractResponse")]
System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Multiply", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/MultiplyResponse")]
double Multiply(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Multiply", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/MultiplyResponse")]
System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Divide", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/DivideResponse")]
double Divide(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/Divide", ReplyAction="http://CoreWcf.Samples.AuthorizationPolicy/ICalculatorService/DivideResponse")]
System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
public interface ICalculatorServiceChannel : CoreWcf.Samples.AuthorizationPolicy.ICalculatorService, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
public partial class CalculatorServiceClient : System.ServiceModel.ClientBase<CoreWcf.Samples.AuthorizationPolicy.ICalculatorService>, CoreWcf.Samples.AuthorizationPolicy.ICalculatorService
{

/// <summary>
/// Implement this partial method to configure the service endpoint.
/// </summary>
/// <param name="serviceEndpoint">The endpoint to configure</param>
/// <param name="clientCredentials">The client credentials</param>
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), CalculatorServiceClient.GetEndpointAddress(endpointConfiguration))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}

public double Add(double n1, double n2)
{
return base.Channel.Add(n1, n2);
}

public System.Threading.Tasks.Task<double> AddAsync(double n1, double n2)
{
return base.Channel.AddAsync(n1, n2);
}

public double Subtract(double n1, double n2)
{
return base.Channel.Subtract(n1, n2);
}

public System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2)
{
return base.Channel.SubtractAsync(n1, n2);
}

public double Multiply(double n1, double n2)
{
return base.Channel.Multiply(n1, n2);
}

public System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2)
{
return base.Channel.MultiplyAsync(n1, n2);
}

public double Divide(double n1, double n2)
{
return base.Channel.Divide(n1, n2);
}

public System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2)
{
return base.Channel.DivideAsync(n1, n2);
}

public virtual System.Threading.Tasks.Task OpenAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_ICalculatorService))
{
System.ServiceModel.WSHttpBinding result = new System.ServiceModel.WSHttpBinding();
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
result.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
return result;
}
if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_ICalculatorService1))
{
System.ServiceModel.WSHttpBinding result = new System.ServiceModel.WSHttpBinding();
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
result.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Certificate;
return result;
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_ICalculatorService))
{
return new System.ServiceModel.EndpointAddress("https://localhost:5001/CalculatorService/Username");
}
if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_ICalculatorService1))
{
return new System.ServiceModel.EndpointAddress("https://localhost:5001/CalculatorService/Certificate");
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

public enum EndpointConfiguration
{

WSHttpBinding_ICalculatorService,

WSHttpBinding_ICalculatorService1,
}
}
}
70 changes: 70 additions & 0 deletions Extensibility/Security/AuthorizationPolicy/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Security;

//The service contract is defined using Connected Service "WCF Web Service", generated from the service by the dotnet svcutil tool.

// Create a client with Username endpoint configuration
WSHttpBinding binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
var endpointAddress = new EndpointAddress("https://localhost:5001/CalculatorService/Username");

CalculatorServiceClient client = new CalculatorServiceClient(binding, endpointAddress);
client.ClientCredentials.UserName.UserName = "test1";
client.ClientCredentials.UserName.Password = "1test";

CallServiceOperations(client);

// Create a client with Certificate endpoint configuration
binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
endpointAddress = new EndpointAddress("https://localhost:5001/CalculatorService/Certificate");

client = new CalculatorServiceClient(binding, endpointAddress);
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "test1");
client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;

CallServiceOperations(client);

Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();


void CallServiceOperations(CalculatorServiceClient client)
{
try
{
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
}
catch (Exception e)
{
Console.WriteLine("Call failed : {0}", e.Message);
}
//Closing the client gracefully closes the connection and cleans up resources
client.CloseAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32422.2
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Service", "Service\Service.csproj", "{BF126326-3393-407C-B24A-8FCCC388BE27}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{B533CADA-93BB-40E1-8FBA-FE37100062C3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolicyLibrary", "PolicyLibrary\PolicyLibrary.csproj", "{7D922471-13A2-4C41-9113-914B475B903D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.Build.0 = Release|Any CPU
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.Build.0 = Release|Any CPU
{7D922471-13A2-4C41-9113-914B475B903D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7D922471-13A2-4C41-9113-914B475B903D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D922471-13A2-4C41-9113-914B475B903D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D922471-13A2-4C41-9113-914B475B903D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AD996EFD-70DC-4431-B411-5A2771DD02D3}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions Extensibility/Security/AuthorizationPolicy/GetComputerName.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'
' This script uses WMI to get the name of the machine be used as the CN ' for the certificates for WCF security samples.
'
set wmi = Getobject("winmgmts:")
wql = "select * from win32_computersystem"
set results = wmi.execquery(wql)
for each compsys in results
'check if the machine is in the workgroup or domain
if compsys.PartOfDomain = 0 or compsys.Domain = compsys.Workgroup then
' only get the name of the machine
WScript.echo compsys.name
else
' get the fully qualified name of the machine
n = compsys.name & "." & compsys.domain
WScript.echo n
end if
next
Loading