Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ public SessionMiddleware(RequestDelegate next, ILogger<SessionMiddleware> logger
}

public Task InvokeAsync(HttpContextCore context)
=> context.GetEndpoint()?.Metadata.GetMetadata<SessionAttribute>() is { Behavior: not SessionBehavior.None } metadata
{
var feature = context.Features.Get<ISessionStateFeature>();
var metadata = context.GetEndpoint()?.Metadata.GetMetadata<SessionAttribute>();

if (feature != null && feature.State != SessionStateBehavior.Default)
{
// override metadata
metadata = metadata ?? new SessionAttribute();
switch (feature.State)
{
case SessionStateBehavior.Disabled:
metadata.Behavior = SessionBehavior.None;
break;
case SessionStateBehavior.ReadOnly:
metadata.IsReadOnly = true;
break;
case SessionStateBehavior.Required:
metadata.Behavior = SessionBehavior.Preload;
break;
}
}

return metadata is { Behavior: not SessionBehavior.None }
? ManageStateAsync(context, metadata)
: _next(context);
}


private async Task ManageStateAsync(HttpContextCore context, SessionAttribute metadata)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NETCOREAPP
using System.Web.SessionState;

namespace Microsoft.AspNetCore.SystemWebAdapters.SessionState;

internal interface ISessionStateFeature
{
SessionStateBehavior State { get; set; }
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ internal HttpContext() { }
public void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void RewritePath(string filePath, string pathInfo, string queryString) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior sessionStateBehavior) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
object System.IServiceProvider.GetService(System.Type service) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public partial class HttpContextBase : System.IServiceProvider
Expand Down Expand Up @@ -184,6 +185,7 @@ public partial class HttpContextBase : System.IServiceProvider
public virtual void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public virtual void RewritePath(string filePath, string pathInfo, string queryString) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public virtual void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public virtual void SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior sessionStateBehavior) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public partial class HttpContextWrapper : System.Web.HttpContextBase
{
Expand All @@ -209,6 +211,7 @@ public partial class HttpContextWrapper : System.Web.HttpContextBase
public override void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void RewritePath(string filePath, string pathInfo, string queryString) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void RewritePath(string filePath, string pathInfo, string queryString, bool setClientFilePath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior sessionStateBehavior) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public sealed partial class HttpCookie
{
Expand Down Expand Up @@ -817,6 +820,13 @@ internal HttpSessionState() { }
public void Remove(string name) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void RemoveAll() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public enum SessionStateBehavior
{
Default = 0,
Disabled = 3,
ReadOnly = 2,
Required = 1,
}
public enum SessionStateMode
{
Custom = 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.Configuration.HttpCapabilitiesBase))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.Hosting.HostingEnvironment))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.SessionState.HttpSessionState))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.SessionState.SessionStateBehavior))]
[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.SessionState.SessionStateMode))]
14 changes: 14 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features.Authentication;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Internal;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand Down Expand Up @@ -151,6 +153,18 @@ public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target)
_context.Response.RegisterForDispose(token);
return token;
}
public void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior)
{
if (_context.Features.Get<ISessionStateFeature>() is { } feature)
{
feature.State = sessionStateBehavior;
}
else
{
var newFeature = new SessionStateFeature() { State = sessionStateBehavior };
_context.Features.Set<ISessionStateFeature>(newFeature);
}
}

[return: NotNullIfNotNull(nameof(context))]
public static implicit operator HttpContext?(HttpContextCore? context) => context?.GetAdapter();
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using System.Security.Principal;
using System.Diagnostics.CodeAnalysis;
using System.Web.SessionState;
using Microsoft.AspNetCore.SystemWebAdapters;

namespace System.Web
Expand Down Expand Up @@ -67,5 +68,7 @@ public virtual IPrincipal User
public virtual void RewritePath(string filePath, string pathInfo, string? queryString) => throw new NotImplementedException();

public virtual void RewritePath(string filePath, string pathInfo, string? queryString, bool setClientFilePath) => throw new NotImplementedException();

public virtual void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior) => throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@ public override IPrincipal User
public override void RewritePath(string filePath, string pathInfo, string? queryString) => _context.RewritePath(filePath, pathInfo, queryString);

public override void RewritePath(string filePath, string pathInfo, string? queryString, bool setClientFilePath) => _context.RewritePath(filePath, pathInfo, queryString, setClientFilePath);

public override void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior) => _context.SetSessionStateBehavior(sessionStateBehavior);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Web.SessionState;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;

namespace Microsoft.AspNetCore.SystemWebAdapters.Internal
{
internal class SessionStateFeature: ISessionStateFeature
{
public SessionStateBehavior State { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Web.SessionState;

public enum SessionStateBehavior
{
Default = 0,
Required = 1,
ReadOnly = 2,
Disabled = 3
};