Coder Social home page Coder Social logo

Session in ASP.NET WebForm is cleared after authenticated by Entra ID with CookieAuthentication and WsFederationAuthentication about aspnetkatana HOT 2 CLOSED

yooontheearth avatar yooontheearth commented on July 30, 2024
Session in ASP.NET WebForm is cleared after authenticated by Entra ID with CookieAuthentication and WsFederationAuthentication

from aspnetkatana.

Comments (2)

Tratcher avatar Tratcher commented on July 30, 2024

@yooontheearth unfortunately this product is not under active development. We may consider PRs, but don't expect feature work or investigations at this point. I recommend StackOverflow for this class of question.

from aspnetkatana.

yooontheearth avatar yooontheearth commented on July 30, 2024

@Tratcher thank you for letting me know about it. I'll ask at StackOverflow then.

Edit
I could make Session work as using PreApplicationStartMethod and HttpModule instead of doing stuff in Global.asax. I'll share the code for those who might need it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Resources;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using System.Web.UI;
using Newtonsoft.Json.Serialization;
using System.Web.SessionState;

[assembly: PreApplicationStartMethod(typeof(AppStartup), "Initialize")]

namespace Test
{
    public static class AppStartup
    {
        // Equivalent to protected void Application_Start(object sender, EventArgs e) in Global.asax
        public static void Initialize()
        {
            // Do something for start app if you want

            // Register a module to do something on every request
            HttpApplication.RegisterModule(typeof(AppHttpModule));
        }
    }

    internal sealed class AppHttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PostAuthorizeRequest += Context_PostAuthorizeRequest;   
        }

        private void Context_PostAuthorizeRequest(object sender, EventArgs e)
        {
			// Enable session if it's for web api
            if (IsWebApiRequest())
            {
                System.Web.HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
            }
        }

        private bool IsWebApiRequest()
        {
            return System.Web.HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith("~/api");
        }

        public void Dispose()
        {
        }
    }
}

from aspnetkatana.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.