3

I'm coding an authentication service that have multiple methods.one of this method is ChangePassword. I want when any body wants to change the password, logon to system before.for this I want to have a session id and before changing pass check it.

How I can do that and has this session time out?

EDIT 1)

I write this code but my session is null every time I want get it's value:

Class:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service2 : IService2
{
    string result
    {   // Store result in AspNet session.
        get
        {
            if (HttpContext.Current.Session["Result"] != null)
                return HttpContext.Current.Session["Result"].ToString();
            return "Session Is Null";
        }
        set
        {
            HttpContext.Current.Session["Result"] = value;
        }
    }

    public void SetSession(string Val)
    {
        result = Val;
    }

    public string GetSession()
    {
        return result;
    }

interface:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService2
{
    [OperationContract]
    void SetSession(string Val);

    [OperationContract]
    string GetSession();
}

web.config

  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

EDIT 2) I wrote this code but it don't work:

 private void button1_Click(object sender, EventArgs e)
    {
        MyService2.Service2Client srv = new MyService2.Service2Client();
        textBox1.Text = srv.GetSession();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        MyService2.Service2Client srv = new MyService2.Service2Client();
        srv.SetSession(textBox1.Text);
        textBox1.Clear();
    }

every time I want to get Session value I get "Session Is Null" .Why?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Arian
  • 12,793
  • 66
  • 176
  • 300
  • Maybe like this: after checking credentials during login, server generates, saves to database and returns to client sessionID. After that client always pass sessionID to server where you can check it? – Renatas M. May 25 '11 at 08:31
  • @Reniuz: In that case why you need a session id specifically? A plain old GUID would do more than enough! – Ε Г И І И О May 10 '12 at 12:15
  • @LOSTCODER And where I wrote that session id is not GUID? – Renatas M. May 10 '12 at 12:31

3 Answers3

6

In order to have SessionId, you have to have Session-enabled binding. For example, wsHttpBinding. In your config file, you should have something like:

  <services>
  <service name="MyService">
    <endpoint address="" binding="wsHttpBinding"
    bindingConfiguration="WSHttpBinding_MyServiceConfig"
    contract="IMyService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </service>
  </services>

In the IMyService interface, you have to have SessionMode attribute set to Required, like so:

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IMyService
{
    [OperationContract]
    AuthenticationData Authenticate(string username, string password);
}

When all of this is setup, you can get to the SessionId like this:

var sessionId = OperationContext.Current.SessionId;

Another way would be to enable AspNetCompatibilityRequirements but it's a bit of an overkill just to get the SessionId.

veljkoz
  • 8,384
  • 8
  • 55
  • 91
  • @veljkoz:thanks but this session Id reset per call.I want a session like session in asp.net.I want when a user create instance of a service a session create and remain for example 20 min.how I can acheive this? – Arian May 25 '11 at 18:47
  • You can have this by not closing the session - while it's open it will keep the session (ie. don't close the service channel after each call but create some singleton class that's providing this service, and recreates it if channel dropped for some reason). We have this exact same scenario and it works like a charm for all of our projects. – veljkoz May 25 '11 at 22:27
  • if I don't close service many channels may be open and this may cause IIS don't handle new requests.is it true? – Arian May 26 '11 at 05:01
  • Well, we had about 300 clients working at the same time, and it worked quite well. You might have to increase the: `` but even with 30 it works for us. On another note - I don't think you can have the SessionId preserved if you close the channel, even in asp.net... so if you rely on sessions, you have to keep them open. – veljkoz May 26 '11 at 07:41
  • I don't close service but it does not work :(:( Please see Edit 2 – Arian May 27 '11 at 15:12
  • wsHttpBinding does not support sessions? – Arian May 29 '11 at 18:52
  • It does - see this answer: http://stackoverflow.com/questions/2650738/how-to-enable-wcf-session-with-wshttpbidning-with-transport-only-security/2650921#2650921 (in short, you probably have to add: ``) – veljkoz May 29 '11 at 21:51
1

When use wsHttpBinding in WCF you will find the value of OperationContext.Current.SessionId is null. The Solution is as follows(need two steps):

  1. Enable reliableSession to true in the configuration file

    <bindings>
      <wsHttpBinding>
        <binding name ="WSHttpBinding_MyService" sendTimeout="00:05:00" >
          <security mode="None"></security>
          <reliableSession enabled="true"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    
  2. In the contract interface, have SessionMode attribute set to Required

    [ServiceContract(SessionMode = SessionMode.Required)]
    public interface IMyService{...}
    

Follow the steps above, the problem will be solved

BigBoss
  • 63
  • 1
  • 6
0

You can activate the ASP.NET compatibility mode in your WCF service, and have all the benefits of ASP.NET sessions and context.

Add this attribute to your WCF class definition:

[AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Required)]

and in your web.config:

<configuration>
  <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>
Mart
  • 5,608
  • 3
  • 32
  • 45