Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a server using console application.
In client side I have two sessions Session["Name"], Session["UserId"]. What I need is the code to get session name and its value in server using C# console application?

What I have tried:

I will get the cookie to identify the client, but not getting where the session name and id is saved in the request
Posted
Updated 15-Jun-17 21:16pm

1 solution

Sessions aren't client side, they are Server side - as is all C# code. Only Javascript is executed at the client, C# and VB are always executed on the server and have no direct access to client resources.

So to access Session variables on the server side is trivial:
C#
string userName = Session["Name"].ToString();
You can access Server variables on the Client side in Javascript:
JavaScript
<script type="text/javascript">
    function showName() {
        alert('<%=Session["Name"].ToString() %>');
    }
</script>
 
Share this answer
 
Comments
Member 10752313 16-Jun-17 3:24am    
Actually I am developing a server, my question is how Session["Name"] set in web form is accessed in my server to create unique id, ie how the name-value pair created in web form is send to server?
OriginalGriff 16-Jun-17 3:34am    
Don't try to type as little as possible: we only get exactly what you type to work with, we can't see your screen, access your HDD, or read your mind!

On it's own, than sentence makes no sense: if the session variable has been created - and it doesn't matter where it was created from - then two things apply.
1) It's in the Session collection, to C# code can access it directly.
2) It's created by Server code, even if that code is executed on Page Load as part of the HTML generation process as in the alert message in the example I gave.
Member 10752313 16-Jun-17 3:46am    
Ok.
I will make it clear.


I have created a unique id for each client request in server as
Guid clientId = Guid.NewGuid();
Cookie setCookie = new Cookie("ASPSESSIONID", clientId.ToString());
HttpListenercontext.Response.AppendCookie(setCookie);


If a client set Session["Name"]=Roy; How can I access the name Roy in my server using HttpListenerContext?
OriginalGriff 16-Jun-17 3:57am    
Um. You do realize that you show no connection between "Roy" and your Guid value? And without some idea of how they are connected - and particularly how they are permanently stored - we can have no idea how to retrieve them later?
Why not store the name in your cookie as well? Or access your DB using the Guid?
F-ES Sitecore 16-Jun-17 4:41am    
Typically the session only exists on the server, the client is never going to set session variables. As Griff says, if you want a mechanism where the client and server can both share state then storing that data in cookies is the way to go if the state needs persisted. If the data is simply to pass from client to server then it is done so via the querystring or form variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900