locked
How share session between two web applications -, ASP.Net Web Forms and ASP.Net MVC RRS feed

  • Question

  • User1215413493 posted

    How share session between two web applications -, ASP.Net Web Forms and ASP.Net MVC

    Tuesday, September 12, 2017 4:35 AM

All replies

  • User-1508012637 posted

    Hi,

    How share session between two web applications -, ASP.Net Web Forms and ASP.Net MVC

    You could make it by persisting session state into SQL Server database so that the session could be shared on different web applicaitons. Please refer to the following steps:

    1. Open Developer Command Prompt of Visual Studio, execute the following command to generate the database to store the session state:

    aspnet_regsql.exe -S localhost -E -ssadd -sstype p

    And the ASPState database would be generated as below in your SQL Server:


    More about ASP.NET SQL Server Registration Tool, see:

    https://msdn.microsoft.com/en-us/library/ms229862.aspx?f=255&MSPPError=-2147217396 

    2. Modify the TempGetAppID Stored Procedure to make sure all web applications have the same AppId so the session would be shared:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    
    ALTER PROCEDURE [dbo].[TempGetAppID]
    @appName tAppName,
    @appId int OUTPUT
    AS
    SET @appName = LOWER('My Session State')
    SET @appId = 1
    
    SELECT @appId = AppId
    FROM dbo.ASPStateTempApplications
    WHERE AppId = 1
    
    IF @appId IS NULL BEGIN
            INSERT dbo.ASPStateTempApplications
            VALUES (@appId, @appName)
    END
    
    RETURN 0

    3. Add the following line in system.web node to web.config file of your two web applications:

    <sessionState mode="SQLServer" sqlConnectionString ="data source=.; Integrated Security=True" timeout="20" />

    4. Complete. You could now test by saving a value to session state in ASP.NET MVC application and you would find that the session state would be persisted into SQL Server database. And then you could read from session state through the same session key in your ASP.NET WebForm application.

    And please notice that when you use a session-state mode other than InProc, the session-variable type must be either a primitive .NET type or serializable. This is because the session-variable value is stored in an external data store. For more information, see Session-State Modes.

    Besides, if you want a user logs in with a single ID and password to gain access to a connected system or systems without using different usernames or passwords, you could try Singe sign-on.

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Tuesday, September 12, 2017 8:24 AM
  • User1215413493 posted

    Thank You for your reply,

    I followed the steps as you mentioned, But i am not able to access the session variable in ASP.Net MVC  application in ASP.Net Forms  applicatiion,

     But i am to access the  session variable in the ASP.Net MVC application in the other ASP.Net MVC application.

    Problem is accessing Session in ASP.Net MVC application in the ASP.Net Forms Application

    Tuesday, September 12, 2017 9:08 AM
  • User-1508012637 posted

    Hi,

    I've tested and it works fine on both ASP.NET MVC and ASP.NET WebForm. Have you added the following line to web.config file into your ASP.NET WebForm application?

    <sessionState mode="SQLServer" sqlConnectionString ="data source=.; Integrated Security=True" timeout="20" />

    Best Regards
    Velen

    Tuesday, September 12, 2017 9:56 AM
  • User1215413493 posted

    yes , i added the same sessionstate sql connectionstring in the both asp.net mvc and asp.net webforms appication

    <sessionState mode="SQLServer" sqlConnectionString ="data source=XXXX;User ID=XXXX;Password=XXXX" timeout="20" />

    Tuesday, September 12, 2017 10:08 AM
  • User-1508012637 posted

    Hi Naga Varma,

    yes , i added the same sessionstate sql connectionstring in the both asp.net mvc and asp.net webforms appication

    <sessionState mode="SQLServer" sqlConnectionString ="data source=XXXX;User ID=XXXX;Password=XXXX" timeout="20" />

    Well, in that case you could get value from SQL Server manually. Please refer to the following steps:

    1. Generate entity class of ASPStateTempSessions table.

    Right click the project, click "Add Item", then choose "ADO.NET Entity Data Model", and select "Code First from database". Then select the ASPStateTempSessions table in ASPState database. Click finish and you'll get the entity class of ASPStateTempSession table.

    2. Use the following code to get session value:

    public object GetSession(ASPStateTempSession sentity, string sessionkey)
    {
        object obj = null;
    
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
        stream.SetLength(0);
    
        if (sentity.SessionItemShort != null)
        {
            stream.Write(sentity.SessionItemShort, 0, sentity.SessionItemShort.Length);
        }
        else
        {
            stream.Write(sentity.SessionItemLong, 0, sentity.SessionItemLong.Length);
        }
    
        stream.Seek(0, System.IO.SeekOrigin.Begin);
        reader.ReadInt32();
        bool bol_flag = reader.ReadBoolean();
        reader.ReadBoolean();
        if (bol_flag)
        {
            System.Web.SessionState.SessionStateItemCollection sessionItems = System.Web.SessionState.SessionStateItemCollection.Deserialize(reader);
            foreach (string key in sessionItems.Keys) //sessionidsession  
            {
                if (sessionkey.Equals(key))
                {
                    obj = sessionItems[key];
                    break;
                }
            }
        }
    
        return obj;
    }

    The first parameter is ASPStateTempSession, the second is the session key string in ASP.NET WebForm. For example, I saved a string to session state with the following code:

    Session["Name"] = "Velen";

    So I could get the string with the following code in ASP.NET MVC, and vice versa:

    SessionStateEntity db = new SessionStateEntity();
    ASPStateTempSession aSPStateTempSession = db.ASPStateTempSessions.Find("zpvw0xqxyns5u3abi5p3iwod00000001"); //get entity based on the SessionId(key)
    string valueFromSession = GetSession(aSPStateTempSession, "Name").ToString(); //replace anywhere you need to get session value, eg.string valueFromSession = session["Name"].ToString()

    Then I'll get "Velen" in valueFromSession.

    If you have any other questions, please feel free to contact me any time.

    Best Regards
    Velen

    Wednesday, September 13, 2017 2:26 AM
  • User753101303 posted

    Hi,

    If I remember you also have to configure an application name inside the web.config file though I can't find this right now (or I am confusing that with something else).

    Try maybe to check the db to see if you see a single application id or two different application ids.

    Also do that it if really make sense. If this is to transition from an app to another note that you can use both Web Forms and MVC in a single application. If you don't have to share all, I would perhpaps do that another way and if you share all then it seems it should be logically a single application. Doing this kind of full session state sharing accross applications would be liekly my very last resort depending on my exact objective.

    Monday, September 18, 2017 4:56 PM
  • User-265582960 posted

    Hi,

    I tried the solution with same application but configure in IIS for 2 domains , like test.xxx.com.au and live.xxx.com.au.

    and I access them from same browse, but the session ID for 2 applications are different even the appid same. so I still cannot share the session.

    how can this workable?

    Saturday, January 20, 2018 11:36 PM
  • User1120430333 posted

    Hi,

    I tried the solution with same application but configure in IIS for 2 domains , like test.xxx.com.au and live.xxx.com.au.

    and I access them from same browse, but the session ID for 2 applications are different even the appid same. so I still cannot share the session.

    how can this workable?

    You can't and you need to abandon this.

    Tuesday, January 23, 2018 1:03 PM
  • User1170841473 posted

    You could make it by persisting session state into SQL Server database so that the session could be shared on different web applicaitons. Please refer to the following steps:

    Hi,

    I followed the steps, and it worked on my local IIS. But when I moved the applications to our Azure virtual machine, the session variables are not shared anymore. 

    I have a WebForms application, and under its root folder I also have an MVC application which should be able to consume the session variables set by the WebForms application.

    Please advise.

    Thanks.

    Wednesday, July 25, 2018 12:49 PM