我怎样才能在WCF服务的会话ID [英] How I can have a session id in wcf Service

查看:109
本文介绍了我怎样才能在WCF服务的会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码有此方法的多个methods.one认证服务的ChangePassword。我想在任何机构想改密码,登录到系统before.for这一点,我想有一个会话ID和之前变化传递检查。

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?

修改1)

我写代码,但我会为空我想每次都遇到它的值:

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

类:

[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

web.config

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



编辑2)
我写了这个代码,但不要'T的工作:

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();
    }



我要得到会议的价值,我得到会话IS NULL每次。为什么呢?

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

推荐答案

为了有SessionID的,你必须有会话启用绑定。例如,的wsHttpBinding 。在您的配置文件,你应该是这样的:

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>

IMyService 界面,你必须已 SessionMode 属性设置为要求,就像这样:

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);
}

当所有这一切都是设置,你可以在的SessionID 是这样的:

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

var sessionId = OperationContext.Current.SessionId;



另一种方法是,使AspNetCompatibilityRequirements,但它是一个有点矫枉过正刚刚拿到的SessionID。

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

这篇关于我怎样才能在WCF服务的会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆