Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into aspnetcore_8
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson committed Dec 12, 2023
2 parents a654454 + 8f2b3a1 commit 9c095ba
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<DeterministicSourcePaths Condition="'$(BUILD_BUILDID)' != '' and '$(IsFrameworkProject)' == 'true'">true</DeterministicSourcePaths>
<ContinuousIntegrationBuild Condition="'$(BUILD_BUILDID)' != '' and '$(IsFrameworkProject)' == 'true'">true</ContinuousIntegrationBuild>

<!-- Use csharp 10.0 for all projects -->
<LangVersion Condition="'$(MSBuildProjectExtension)' == '.csproj'">11.0</LangVersion>
<!-- Use csharp 12.0 for all projects -->
<LangVersion Condition="'$(MSBuildProjectExtension)' == '.csproj'">12.0</LangVersion>
<AnalysisMode Condition="'$(IsFrameworkProject)' == 'true'">Recommended</AnalysisMode>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>

Expand Down Expand Up @@ -74,7 +74,7 @@
<!-- Enable source link, we really only want this for framework assemblies and CI builds
-->
<ItemGroup Condition="'$(IsFrameworkProject)' == 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>

</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Version>1.0.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
<RootNamespace>OpenRiaServices.Hosting</RootNamespace>
<!-- Ignore documentation varnings while experimental-->
<NoWarn>$(NoWarn);CS1574;CS1573;CS1591;CS1572</NoWarn>
<VersionPrefix>0.4.0</VersionPrefix>
<VersionPrefix>1.0.0</VersionPrefix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Daniel-Svensson</Authors>
<PackageTags>OpenRiaServices AspNetCore Hosting DomainServices AspNet WCF RIA Services Server</PackageTags>
<Description>
Asp.Net Core Hosting for OpenRiaservices
Public API will change before 1.0.0 release

See README at https://github.com/OpenRIAServices/OpenRiaServices/tree/main/src/OpenRiaServices.Hosting.AspNetCore/Framework for usage.
</Description>
Expand All @@ -26,6 +25,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" Condition="'$(TargetFramework)' != 'net8.0'" />
</ItemGroup>

<ItemGroup>
Expand Down
79 changes: 78 additions & 1 deletion src/OpenRiaServices.Hosting.AspNetCore/Framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ https://github.com/OpenRIAServices/OpenRiaServices/blob/086ea8c8fcb115000749be6b

Minimal program:

```
```csharp
using OpenRiaServices.Hosting.AspNetCore;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -93,6 +93,83 @@ You can still (and probably should) use the OpenRiaServices specific attributes



### AspNetCore Authentication and Authorization

You can use standard [aspnetcore Authentication and Authorization](https://learn.microsoft.com/en-us/aspnet/core/security/?view=aspnetcore-7.0)
to validate most requests (but **NOT** indivudual Insert, Update, Delete methods, you can apply atttributes to your DomainService class for those).
The validation will happen before the DomainService is even created, making them more powerful than built in attributes.

The **[AspNetCore Sample](https://github.com/OpenRIAServices/Samples/tree/main/WpfCore_AspNetCore)** shows how cookie based login similar to ASP.NET Membership provider *can* be handled.
The change was added in [#16: Add AspNetCore AuthenticationService](https://github.com/OpenRIAServices/Samples/pull/16)
You will need to tweak it so it validates credentials, assign correct Claims (Roles) to users and fits your choosen scheme for authentication.


#### Authentication: Client setup
For the client, **you need to ensure that all HttpClients share the same CookieContainer** and that the it is set to use to cookies (https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.usecookies?view=net-7.0#system-net-http-httpclienthandler-usecookies)

#### Authentication: Asp.Net Core Setup Authentication and Authorization setup

In Program.cs the following code is needed

Service registrations, adds cookie based authentication and enable Authorization
```csharp
// Additional dependencies for cookie based AuthenticationService
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
builder.Services.AddHttpContextAccessor();

builder.Services.AddAuthorization();
```

After the "application has been built" and the middleware is configured you should add Authentication (and Authorization if used).
They should be added **before** *OpenRiaServices*

```csharp
// Add authentication
app.UseAuthentication();
app.UseAuthorization();

// Enable OpenRiaServices
app.MapOpenRiaServices(....
```

#### Authorization: Protecting DomainServices using Authorization middleware

A good idea is to protect your DomainServices, all of them or individual, by requiring authorization.
The [Authorization middleware](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/introduction) should run before the DomainService is created so you will avoid any cost with creating the DomainService and it's dependencies, providing a much better protection against DOS.
**IMPORANT**: error message on the client will be different and not as user friendly as if the `[RequiresAuthentication]` attribute is used.
You might want to consider changing the HTTP status code to match 401 or similar instead of 404 (when it tries to redirect to missing "/Account/Login" endpoint)
**Note**: If you need to selectivly only require authentication for some Submit operations (Insert, Update, Delete or Entity Action) then remember to use `[RequiresAuthentication]`

**Require request to all DomainServices be authorized using default authorization policy**:

```csharp
app.MapOpenRiaServices(builder =>
{
builder.AddDomainService<SampleDomainService>();
builder.AddDomainService<MyAuthenticationService>();
}).RequireAuthorization();
```

You can also control the setting per DomainService using code
```csharp
app.MapOpenRiaServices(builder =>
{
builder.AddDomainService<SampleDomainService>()
.RequireAuthorization();
});
```

You can also control the setting per DomainService using attributes
```csharp
[Authorize]
public class MyAuthenticationService : DomainService, IAuthentication<MyUser>
{
[AllowAnonymous]
public MyUser GetUser() {...}
```


### Output Cache Integration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,48 @@
//------------------------------------------------------------
using OpenRiaServices;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Frozen;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml;

#pragma warning disable 1634, 1691
namespace System.ServiceModel.Dispatcher
{
// Thread Safety: This class is thread safe
class QueryStringConverter
{
private readonly HashSet<Type> _defaultSupportedQueryStringTypes = new();
private readonly FrozenSet<Type> _defaultSupportedQueryStringTypes;
// the cache does not have a quota since it is per endpoint and is
// bounded by the number of types in the contract at the endpoint
private readonly ConcurrentDictionary<Type, TypeConverter> _typeConverterCache = new();

public QueryStringConverter()
{
this._defaultSupportedQueryStringTypes.Add(typeof(byte));
this._defaultSupportedQueryStringTypes.Add(typeof(sbyte));
this._defaultSupportedQueryStringTypes.Add(typeof(short));
this._defaultSupportedQueryStringTypes.Add(typeof(int));
this._defaultSupportedQueryStringTypes.Add(typeof(long));
this._defaultSupportedQueryStringTypes.Add(typeof(ushort));
this._defaultSupportedQueryStringTypes.Add(typeof(uint));
this._defaultSupportedQueryStringTypes.Add(typeof(ulong));
this._defaultSupportedQueryStringTypes.Add(typeof(float));
this._defaultSupportedQueryStringTypes.Add(typeof(double));
this._defaultSupportedQueryStringTypes.Add(typeof(bool));
this._defaultSupportedQueryStringTypes.Add(typeof(char));
this._defaultSupportedQueryStringTypes.Add(typeof(decimal));
this._defaultSupportedQueryStringTypes.Add(typeof(string));
this._defaultSupportedQueryStringTypes.Add(typeof(object));
this._defaultSupportedQueryStringTypes.Add(typeof(DateTime));
this._defaultSupportedQueryStringTypes.Add(typeof(TimeSpan));
this._defaultSupportedQueryStringTypes.Add(typeof(byte[]));
this._defaultSupportedQueryStringTypes.Add(typeof(Guid));
this._defaultSupportedQueryStringTypes.Add(typeof(Uri));
this._defaultSupportedQueryStringTypes.Add(typeof(DateTimeOffset));
this._defaultSupportedQueryStringTypes = FrozenSet.ToFrozenSet(new Type[] {
typeof(byte),
typeof(sbyte),
typeof(short),
typeof(int),
typeof(long),
typeof(ushort),
typeof(uint),
typeof(ulong),
typeof(float),
typeof(double),
typeof(bool),
typeof(char),
typeof(decimal),
typeof(string),
typeof(object),
typeof(DateTime),
typeof(TimeSpan),
typeof(byte[]),
typeof(Guid),
typeof(Uri),
typeof(DateTimeOffset),
});
}

public virtual bool CanConvert(Type type)
Expand Down Expand Up @@ -157,7 +158,7 @@ public virtual string ConvertValueToString(object parameter, Type parameterType)
{
ArgumentNullException.ThrowIfNull(parameterType);
if (parameterType.IsValueType)
ArgumentNullException.ThrowIfNull(parameter);
ArgumentNullException.ThrowIfNull(parameter);

switch (Type.GetTypeCode(parameterType))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<WriteLinesToFile File="ProjectPath.txt" Lines="$(MSBuildProjectFullPath),Generated_Code," Overwrite="true" />
</Target>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Version>1.0.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override MemberExpression ResolveMember(Type type, string member, Express
MemberExpression mex = null;
IDictionary<PropertyDescriptor, IncludeAttribute[]> entityIncludeMap = MetaType.GetMetaType(type).ProjectionMemberMap;

if (entityIncludeMap.Any())
if (entityIncludeMap.Count > 0)
{
// Do a reverse lookup in the include map for the type, looking
// for the source of the member (if present)
Expand Down Expand Up @@ -61,4 +61,4 @@ public override MemberExpression ResolveMember(Type type, string member, Express
return mex;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Compile Include="..\..\OpenRiaServices.Server\Test\MockDataService.cs" Link="Shared\MockDataService.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\OpenRiaServices.Tools\Framework\OpenRiaServices.Tools.csproj" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.6.10" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.6.10" />
<PackageReference Include="Microsoft.Build" Version="17.0.0" PrivateAssets="All" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="17.0.0" PrivateAssets="All" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.0.0" PrivateAssets="All" ExcludeAssets="Runtime" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.0.0" PrivateAssets="All" ExcludeAssets="Runtime" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="7.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 9c095ba

Please sign in to comment.