Skip to content

Commit 2745dd3

Browse files
committed
Add Mixed-auth-AllowAnonymous-workaround
1 parent 2078717 commit 2745dd3

File tree

19 files changed

+661
-0
lines changed

19 files changed

+661
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="OpenIddict.Client" Version="4.0.0" />
12+
<PackageReference Include="OpenIddict.Client.SystemNetHttp" Version="4.0.0" />
13+
<PackageReference Include="System.ServiceModel.Duplex" Version="4.10.*" />
14+
<PackageReference Include="System.ServiceModel.Federation" Version="4.10.*" />
15+
<PackageReference Include="System.ServiceModel.Http" Version="4.10.*" />
16+
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*" />
17+
<PackageReference Include="System.ServiceModel.Security" Version="4.10.*" />
18+
</ItemGroup>
19+
20+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"ExtendedData": {
3+
"inputs": [
4+
"https://localhost:7233/MixedAuthService.svc"
5+
],
6+
"collectionTypes": [
7+
"System.Array",
8+
"System.Collections.Generic.Dictionary`2"
9+
],
10+
"namespaceMappings": [
11+
"*, ServiceReference1"
12+
],
13+
"targetFramework": "net7.0",
14+
"typeReuseMode": "All"
15+
}
16+
}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
namespace ServiceReference1
11+
{
12+
13+
14+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
15+
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IAnonymousService")]
16+
public interface IAnonymousService
17+
{
18+
19+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAnonymousService/EchoAnonymous", ReplyAction="http://tempuri.org/IAnonymousService/EchoAnonymousResponse")]
20+
System.Threading.Tasks.Task<string> EchoAnonymousAsync(string value);
21+
}
22+
23+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
24+
public interface IAnonymousServiceChannel : ServiceReference1.IAnonymousService, System.ServiceModel.IClientChannel
25+
{
26+
}
27+
28+
[System.Diagnostics.DebuggerStepThroughAttribute()]
29+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
30+
public partial class AnonymousServiceClient : System.ServiceModel.ClientBase<ServiceReference1.IAnonymousService>, ServiceReference1.IAnonymousService
31+
{
32+
33+
/// <summary>
34+
/// Implement this partial method to configure the service endpoint.
35+
/// </summary>
36+
/// <param name="serviceEndpoint">The endpoint to configure</param>
37+
/// <param name="clientCredentials">The client credentials</param>
38+
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
39+
40+
public AnonymousServiceClient() :
41+
base(AnonymousServiceClient.GetDefaultBinding(), AnonymousServiceClient.GetDefaultEndpointAddress())
42+
{
43+
this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_IAnonymousService.ToString();
44+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
45+
}
46+
47+
public AnonymousServiceClient(EndpointConfiguration endpointConfiguration) :
48+
base(AnonymousServiceClient.GetBindingForEndpoint(endpointConfiguration), AnonymousServiceClient.GetEndpointAddress(endpointConfiguration))
49+
{
50+
this.Endpoint.Name = endpointConfiguration.ToString();
51+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
52+
}
53+
54+
public AnonymousServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
55+
base(AnonymousServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
56+
{
57+
this.Endpoint.Name = endpointConfiguration.ToString();
58+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
59+
}
60+
61+
public AnonymousServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
62+
base(AnonymousServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
63+
{
64+
this.Endpoint.Name = endpointConfiguration.ToString();
65+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
66+
}
67+
68+
public AnonymousServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
69+
base(binding, remoteAddress)
70+
{
71+
}
72+
73+
public System.Threading.Tasks.Task<string> EchoAnonymousAsync(string value)
74+
{
75+
return base.Channel.EchoAnonymousAsync(value);
76+
}
77+
78+
public virtual System.Threading.Tasks.Task OpenAsync()
79+
{
80+
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
81+
}
82+
83+
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
84+
{
85+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IAnonymousService))
86+
{
87+
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
88+
result.MaxBufferSize = int.MaxValue;
89+
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
90+
result.MaxReceivedMessageSize = int.MaxValue;
91+
result.AllowCookies = true;
92+
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
93+
return result;
94+
}
95+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
96+
}
97+
98+
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
99+
{
100+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IAnonymousService))
101+
{
102+
return new System.ServiceModel.EndpointAddress("https://localhost:7233/MixedAuthService.svc/anonymous");
103+
}
104+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
105+
}
106+
107+
private static System.ServiceModel.Channels.Binding GetDefaultBinding()
108+
{
109+
return AnonymousServiceClient.GetBindingForEndpoint(EndpointConfiguration.BasicHttpBinding_IAnonymousService);
110+
}
111+
112+
private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
113+
{
114+
return AnonymousServiceClient.GetEndpointAddress(EndpointConfiguration.BasicHttpBinding_IAnonymousService);
115+
}
116+
117+
public enum EndpointConfiguration
118+
{
119+
120+
BasicHttpBinding_IAnonymousService,
121+
}
122+
}
123+
124+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
125+
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.ISecuredService")]
126+
public interface ISecuredService
127+
{
128+
129+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAnonymousService/EchoAnonymous", ReplyAction="http://tempuri.org/IAnonymousService/EchoAnonymousResponse")]
130+
System.Threading.Tasks.Task<string> EchoAnonymousAsync(string value);
131+
132+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISecuredService/Echo", ReplyAction="http://tempuri.org/ISecuredService/EchoResponse")]
133+
System.Threading.Tasks.Task<string> EchoAsync(string value);
134+
}
135+
136+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
137+
public interface ISecuredServiceChannel : ServiceReference1.ISecuredService, System.ServiceModel.IClientChannel
138+
{
139+
}
140+
141+
[System.Diagnostics.DebuggerStepThroughAttribute()]
142+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
143+
public partial class SecuredServiceClient : System.ServiceModel.ClientBase<ServiceReference1.ISecuredService>, ServiceReference1.ISecuredService
144+
{
145+
146+
/// <summary>
147+
/// Implement this partial method to configure the service endpoint.
148+
/// </summary>
149+
/// <param name="serviceEndpoint">The endpoint to configure</param>
150+
/// <param name="clientCredentials">The client credentials</param>
151+
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
152+
153+
public SecuredServiceClient() :
154+
base(SecuredServiceClient.GetDefaultBinding(), SecuredServiceClient.GetDefaultEndpointAddress())
155+
{
156+
this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_ISecuredService.ToString();
157+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
158+
}
159+
160+
public SecuredServiceClient(EndpointConfiguration endpointConfiguration) :
161+
base(SecuredServiceClient.GetBindingForEndpoint(endpointConfiguration), SecuredServiceClient.GetEndpointAddress(endpointConfiguration))
162+
{
163+
this.Endpoint.Name = endpointConfiguration.ToString();
164+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
165+
}
166+
167+
public SecuredServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
168+
base(SecuredServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
169+
{
170+
this.Endpoint.Name = endpointConfiguration.ToString();
171+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
172+
}
173+
174+
public SecuredServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
175+
base(SecuredServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
176+
{
177+
this.Endpoint.Name = endpointConfiguration.ToString();
178+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
179+
}
180+
181+
public SecuredServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
182+
base(binding, remoteAddress)
183+
{
184+
}
185+
186+
public System.Threading.Tasks.Task<string> EchoAnonymousAsync(string value)
187+
{
188+
return base.Channel.EchoAnonymousAsync(value);
189+
}
190+
191+
public System.Threading.Tasks.Task<string> EchoAsync(string value)
192+
{
193+
return base.Channel.EchoAsync(value);
194+
}
195+
196+
public virtual System.Threading.Tasks.Task OpenAsync()
197+
{
198+
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
199+
}
200+
201+
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
202+
{
203+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ISecuredService))
204+
{
205+
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
206+
result.MaxBufferSize = int.MaxValue;
207+
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
208+
result.MaxReceivedMessageSize = int.MaxValue;
209+
result.AllowCookies = true;
210+
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
211+
return result;
212+
}
213+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
214+
}
215+
216+
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
217+
{
218+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ISecuredService))
219+
{
220+
return new System.ServiceModel.EndpointAddress("https://localhost:7233/MixedAuthService.svc/authenticated");
221+
}
222+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
223+
}
224+
225+
private static System.ServiceModel.Channels.Binding GetDefaultBinding()
226+
{
227+
return SecuredServiceClient.GetBindingForEndpoint(EndpointConfiguration.BasicHttpBinding_ISecuredService);
228+
}
229+
230+
private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
231+
{
232+
return SecuredServiceClient.GetEndpointAddress(EndpointConfiguration.BasicHttpBinding_ISecuredService);
233+
}
234+
235+
public enum EndpointConfiguration
236+
{
237+
238+
BasicHttpBinding_ISecuredService,
239+
}
240+
}
241+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Net;
5+
using System.ServiceModel;
6+
using System.ServiceModel.Channels;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using OpenIddict.Client;
9+
using ServiceReference1;
10+
11+
// Request an access_token
12+
var services = new ServiceCollection();
13+
services.AddOpenIddict()
14+
.AddClient(options =>
15+
{
16+
options.AllowClientCredentialsFlow();
17+
options.DisableTokenStorage();
18+
options.UseSystemNetHttp();
19+
options.AddRegistration(new OpenIddictClientRegistration
20+
{
21+
Issuer = new Uri("https://localhost:7222/", UriKind.Absolute),
22+
ClientId = "console_app",
23+
ClientSecret = "secret",
24+
Scopes = { "api" }
25+
}
26+
);
27+
});
28+
var provider = services.BuildServiceProvider();
29+
var service = provider.GetRequiredService<OpenIddictClientService>();
30+
var (tokenResponse, _) = await service.AuthenticateWithClientCredentialsAsync(new Uri("https://localhost:7222/", UriKind.Absolute), new [] { "api" });
31+
32+
Console.WriteLine($"Retrieved access_token {tokenResponse.AccessToken}");
33+
34+
// Create an authenticated client and call 'Echo' and 'EchoAnonymous'
35+
var channelFactory = new ChannelFactory<ISecuredServiceChannel>(new BasicHttpBinding(BasicHttpSecurityMode.Transport),
36+
new EndpointAddress("https://localhost:7233/MixedAuthService.svc/authenticated"));
37+
var channel = channelFactory.CreateChannel();
38+
39+
var context = new OperationContext(channel);
40+
using (var _ = new OperationContextScope(context))
41+
{
42+
var httpRequestProperty = new HttpRequestMessageProperty();
43+
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = $"Bearer {tokenResponse.AccessToken}";
44+
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
45+
var response = await channel.EchoAsync("Hello world");
46+
Console.WriteLine(response);
47+
var response2 = await channel.EchoAnonymousAsync("Hey");
48+
Console.WriteLine(response2);
49+
}
50+
51+
// Create an anonymous client and call 'EchoAnonymous'
52+
var anonymousChannelFactory = new ChannelFactory<IAnonymousServiceChannel>(new BasicHttpBinding(BasicHttpSecurityMode.Transport),
53+
new EndpointAddress("https://localhost:7233/MixedAuthService.svc/anonymous"));
54+
55+
var anonymousChannel = anonymousChannelFactory.CreateChannel();
56+
57+
var response3 = await anonymousChannel.EchoAnonymousAsync("Bonjour");
58+
Console.WriteLine(response3);
59+
60+
Console.ReadLine();
61+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="OpenIddict.AspNetCore" Version="4.0.0" />
10+
</ItemGroup>
11+
</Project>

0 commit comments

Comments
 (0)