.Net webservice读取http头 [英] .Net webservice reading http header
问题描述
我有以下情况:
Android应用程序正在调用我的c#webservice。在HTTP标题中,将有一个令牌,我必须在每次调用我的web服务时检查以验证调用者的身份。
我想要强调我的webservice不是WCF服务,它的名字以asmx结尾。
我的一个webmethods的代码如下:
I have the following situation:
An android app is calling my c# webservice. In the HTTP header there will be a token which I have to check each time my webservice is called in order to validate the identity of the caller.
I would like to stress that my webservice is not a WCF service, its name ends with asmx.
The code for one of my webmethods looks like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Object> GetContact(string clientNr, int clientId = 0)
{
var contact = new List<Object>();
...code ommited
return contact;
}
我的想法是拨打电话,例如
My idea is to make a call like
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Object> GetContact(string clientNr, int clientId = 0)
{
If(CheckToken())
{
var contact = new List<Object>();
...code ommited
}
return contact;
}
函数CheckToken将返回True或False。它会从HTTP Header读取令牌,但我该怎么做?
亲切的问候
Jeroen
The function CheckToken would return True or False. It would be reading the token from the HTTP Header, but how do I do that?
Kind regards
Jeroen
推荐答案
您必须创建从soapheader类派生的令牌。
公共类TokenHeader:System.Web.Services。 Protocols.SoapHeader
{
public string tokenNo;
}
in Web服务类使用
公共TokenHeader令牌;
[WebMethod]
[SoapHeader(token,Required = true)]
public string GetToken()
{
if(checkToken())
返回token.tokenNo;
else
返回身份验证错误;
}
private bool checkToken()
{
if(token!= null)
{
i f(token.tokenNo == 100)
返回true;
else
返回false;
}
else
返回false;
}
You will have to create the token derived from soapheader class.
public class TokenHeader : System.Web.Services.Protocols.SoapHeader
{
public string tokenNo;
}
in the web service class use
public TokenHeader token;
[WebMethod]
[SoapHeader("token",Required=true)]
public string GetToken()
{
if (checkToken())
return token.tokenNo;
else
return "Error in authentication";
}
private bool checkToken()
{
if (token!= null)
{
if (token.tokenNo==100)
return true;
else
return false;
}
else
return false;
}
这就是我想出的:
命名空间TestServiceHttpAuthentication
{
公共类AuthenticateRequestHttpModule:IHttpModule
{
私人HttpApplication mHttpApp;
public void Init(HttpApplication httpApp)
{
this.mHttpApp = httpApp;
mHttpApp.AuthenticateRequest + = new EventHandler(OnAuthentication);
}
void OnAuthentication(对象发送者,EventArgs a)
{
HttpApplication application = (HttpApplication)发件人;
HttpRequest request = application.Context.Request;
// WindowsIdentity identity =(WindowsIdentity)application.Context.User.Identity;
StreamWriter sw = new StreamWriter(request.MapPath(Test.txt),true);
sw.WriteLine(application.Context.Request.HttpMethod。 ToString());
sw.WriteLine(application.Context.Request.Headers.Count.ToString());
NameValueCollection coll;
coll = application.Context.Request.Headers;
string [] arr1 = coll.AllKeys;
for(int loop1 = 0; loop1< arr1.length; loop1 ++ )>
{
sw.WriteLine(arr1 [loop1] .ToString());
string [] arr2 = coll.GetValues(arr1 [loop1]);
for(int loop2 = 0; loop2< arr2.length; loop2 ++)>
{
sw.WriteLine(arr2 [loop2] .ToString( ));
}
}
sw.Flush();
sw.Close();
}
public void Dispose( )
{}
}
}
在web.config中:
This is what I came up with:
namespace TestServiceHttpAuthentication
{
public class AuthenticateRequestHttpModule : IHttpModule
{
private HttpApplication mHttpApp;
public void Init(HttpApplication httpApp)
{
this.mHttpApp = httpApp;
mHttpApp.AuthenticateRequest += new EventHandler(OnAuthentication);
}
void OnAuthentication(object sender, EventArgs a)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Context.Request;
//WindowsIdentity identity = (WindowsIdentity)application.Context.User.Identity;
StreamWriter sw = new StreamWriter(request.MapPath("Test.txt"), true);
sw.WriteLine(application.Context.Request.HttpMethod.ToString());
sw.WriteLine(application.Context.Request.Headers.Count.ToString());
NameValueCollection coll;
coll=application.Context.Request.Headers;
string[] arr1 = coll.AllKeys;
for(int loop1=0;loop1<arr1.length;loop1++)>
{
sw.WriteLine(arr1[loop1].ToString());
string[] arr2=coll.GetValues(arr1[loop1]);
for(int loop2=0;loop2<arr2.length;loop2++)>
{
sw.WriteLine(arr2[loop2].ToString());
}
}
sw.Flush();
sw.Close();
}
public void Dispose()
{ }
}
}
And in the web.config:
<httpModules>
add name="AuthenticateRequestHttpModule" type="TestServiceHttpAuthentication.AuthenticateRequestHttpModule, TestServiceHttpAuthentication" />
</httpModules>
现在我可以从HTTP标头读取,验证特定键/值的值对。
感谢那些和我一起想过的人。
Jeroen
Now I can read from the HTTP header, validate the values from specific key/value pairs.
Thanks for those who''ve thought with me.
Jeroen
这篇关于.Net webservice读取http头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!