为我已经工作的 REST 服务启用令牌
Enable Token to my already working REST Service
我已经创建了 REST 服务,它运行良好。当我调用方法时,我会向我发送 Json 响应。
现在我想启用令牌。我编写代码来生成令牌并 运行 它。它给了我以下错误:
合同需要会话,但绑定 'WebHttpBinding' 不支持它或未正确配置以支持它。
请告诉我解决此问题需要进行哪些更改。
网络配置
<?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>
服务Class接口
[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);
}
服务实施Class
[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;
}
}
我找到了另一种解决方案,即我创建了一个自定义(某些值的组合)加密令牌。它工作正常。我必须使用数据库方法来使用它。
我已经创建了 REST 服务,它运行良好。当我调用方法时,我会向我发送 Json 响应。
现在我想启用令牌。我编写代码来生成令牌并 运行 它。它给了我以下错误:
合同需要会话,但绑定 'WebHttpBinding' 不支持它或未正确配置以支持它。
请告诉我解决此问题需要进行哪些更改。
网络配置
<?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>
服务Class接口
[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);
}
服务实施Class
[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;
}
}
我找到了另一种解决方案,即我创建了一个自定义(某些值的组合)加密令牌。它工作正常。我必须使用数据库方法来使用它。