我正在编写一个具有多种方法的身份验证服务.此方法的一部分是ChangePassword.我想当任何机构想要更改密码,之前登录系统.为此我想要一个会话ID并在更改传递之前检查它.
我怎么能这样做,并且会议时间过去了吗?
编辑1)
我写这段代码,但每次我想要得到它的值时,我的会话都是null:
类:
[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;
}
Run Code Online (Sandbox Code Playgroud)
接口:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService2
{
[OperationContract]
void SetSession(string Val);
[OperationContract]
string GetSession();
}
Run Code Online (Sandbox Code Playgroud)
web.config中
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
Run Code Online (Sandbox Code Playgroud)
编辑2) 我写了这段代码,但它不起作用:
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();
}
Run Code Online (Sandbox Code Playgroud)
每次我想获得Session值时,我都会得到"Session is Null".为什么?
要获得SessionId,您必须具有启用会话的绑定.例如,wsHttpBinding
.在配置文件中,您应该具有以下内容:
<services>
<service name="MyService">
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_MyServiceConfig"
contract="IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
在IMyService
界面中,您必须将SessionMode
属性设置为Required
,如下所示:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IMyService
{
[OperationContract]
AuthenticationData Authenticate(string username, string password);
}
Run Code Online (Sandbox Code Playgroud)
完成所有这些后,您可以SessionId
这样:
var sessionId = OperationContext.Current.SessionId;
Run Code Online (Sandbox Code Playgroud)
另一种方法是启用AspNetCompatibilityRequirements但是获得SessionId只是有点过分.