我已经创建了REST服务,它运行正常。当我打电话给方法时,我会给我发送Json回复。
现在我要启用令牌。我编写代码来生成令牌并运行它。它给了我以下错误:
请告诉我解决此问题所需的更改。
<?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;
}
}
答案 0 :(得分:0)
我找到了另一个解决方案,即我创建了一个自定义(某些值的组合)加密令牌。它工作正常。我必须使用数据库方法来使用它。