Wcf 为我已经工作的REST服务启用令牌
我已经创建了REST服务,它工作得很好。当我调用一个方法时,我将向我发送Json响应 现在我想启用令牌。我编写代码来生成令牌并运行它。它给了我以下错误: 协定需要会话,但绑定“WebHttpBinding”不支持会话,或者未正确配置以支持会话。 请告诉我修复此问题所需的更改 网络配置 服务实现类Wcf 为我已经工作的REST服务启用令牌,wcf,wcf-binding,Wcf,Wcf Binding,我已经创建了REST服务,它工作得很好。当我调用一个方法时,我将向我发送Json响应 现在我想启用令牌。我编写代码来生成令牌并运行它。它给了我以下错误: 协定需要会话,但绑定“WebHttpBinding”不支持会话,或者未正确配置以支持会话。 请告诉我修复此问题所需的更改 网络配置 服务实现类 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)] //[服务行为] 公共课服务:IService { strin
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
//[服务行为]
公共课服务:IService
{
string UserToken=string.Empty;
公共bool IsValidateUser()
{
//从客户端请求获取用户令牌
if(OperationContext.Current.IncomingMessageHeaders.FindHeader(“TokenHeader”,“TokenNameSpace”)=-1)
{
返回false;
}
字符串userIdentityToken=Convert.ToString(OperationContext.Current.IncomingMessageHeaders.GetHeader(“TokenHeader”、“TokenNameSpace”);
//使用令牌验证用户(如果已验证),然后返回员工数据
if(userIdentityToken==UserToken)
{
返回true;
}
其他的
{
返回false;
}
}
公共字符串GetStudent()
{
JavaScriptSerializer js=新的JavaScriptSerializer();
返回js.Serialize(Student.GetStudent());
}
公共字符串验证器(字符串用户,字符串pwd)
{
if(!(string.IsNullOrEmpty(user))&&(!(string.IsNullOrEmpty(pwd)))
{
UserToken=OperationContext.Current.SessionId;
}
返回UserToken;
}
}
我找到了另一个解决方案,那就是我创建了一个自定义(一些值的组合)加密令牌。而且效果很好。我必须使用数据库方法来使用它。我找到了另一个解决方案,那就是我创建了一个自定义(一些值的组合)加密令牌。而且效果很好。我必须使用数据库方法来使用它。可能重复的,所以根据这个,我必须替换WebHttpBinding以像REST一样工作。是否有其他方法来维护WebHttpBinding REST服务上的会话。好的,据我所知,REST应该是无状态的。维持一个会话似乎违反直觉。它只用于身份验证吗?然后,也许您可以问一个关于如何在REST端点上进行身份验证的问题。不过,这比先用谷歌搜索要好。我找到了另一个解决方案。我已创建自定义令牌并进行验证。谢谢你的回复。可能是重复的,所以根据这个什么绑定更改,我必须替换WebHttpBinding才能像REST一样工作。有没有其他方法来维护WebHttpBinding REST服务上的会话。嗯,据我所知,REST应该是无状态的。维持一个会话似乎违反直觉。它只用于身份验证吗?然后,也许您可以问一个关于如何在REST端点上进行身份验证的问题。不过,这比先用谷歌搜索要好。我找到了另一个解决方案。我已创建自定义令牌并进行验证。谢谢你的回复。
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="Service.Service" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="webHttpBinding" contract="Service.IService" behaviorConfiguration="web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
[ServiceContract(SessionMode = SessionMode.Required)]
//[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(
Method = "GET",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetStudent")]
string GetStudent();
[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "AuthenticateUser")]
string AuthenticateUser(string user, string pwd);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
//[ServiceBehavior]
public class Service : IService
{
string UserToken = string.Empty;
public bool IsValidateUser()
{
//Getting the user token from client request
if (OperationContext.Current.IncomingMessageHeaders.FindHeader("TokenHeader", "TokenNameSpace") == -1)
{
return false;
}
string userIdentityToken = Convert.ToString(OperationContext.Current.IncomingMessageHeaders.GetHeader<string>("TokenHeader", "TokenNameSpace"));
//Authenticating user with token, if it is validated then returning employee data
if (userIdentityToken == UserToken)
{
return true;
}
else
{
return false;
}
}
public string GetStudent()
{
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(Student.GetStudent());
}
public string AuthenticateUser(string user, string pwd)
{
if (!(string.IsNullOrEmpty(user)) && !(string.IsNullOrEmpty(pwd)))
{
UserToken = OperationContext.Current.SessionId;
}
return UserToken;
}
}