Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

c# - How I can have a session id in wcf Service

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...