How can I have a session id in wcf Service

I am encoding an authentication service that has several methods. One method is ChangePassword. I want, when any authority wants to change the password, log in earlier. To do this, I want to have a session identifier and check it before changing the password.

How can I do this and do you have this session?

EDIT 1)

I am writing this code, but my session is null every time I want to get its value:

Grade:

[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 does not 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 a Session value, I get "Session Is Null" .Why?

+3
source share
3 answers

SessionId, . , wsHttpBinding. - :

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

IMyService SessionMode, Required, :

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

, SessionId :

var sessionId = OperationContext.Current.SessionId;

AspNetCompatibilityRequirements, , SessionId.

+6

wsHttpBinding WCF OperationContext.Current.SessionId null. ( ):

  • reliableSession true

    <bindings>
      <wsHttpBinding>
        <binding name ="WSHttpBinding_MyService" sendTimeout="00:05:00" >
          <security mode="None"></security>
          <reliableSession enabled="true"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    
  • SessionMode

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

, .

+1

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

Add this attribute to the WCF class definition:

[AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Required)]

and in your web.config:

<configuration>
  <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>
0
source

All Articles