具有密码保护的Web服务 [英] Web service with password protection
问题描述
大家好,
我想用C#创建一个Web服务,要求输入有效的用户名和密码。
有办法吗?
请帮忙。
谢谢
Hi All,
I want to create a web service in C#, which asks for a valid user name and password to access it.
Is there a way?
Please help.
Thank you
推荐答案
您可以在服务配置文件中使用userPrincipalName。
You can user userPrincipalName inside the service config file.
<identity> <userPrincipalName value="someone@cohowinery.com" /> </identity>
你也可以用代码打电话。
You can also call by code.
string uri = "http://loaclhost:8989/MyService";
EndpointIdentity epid = EndpointIdentity.CreateUpnIdentity(@"MACHINE\user");
EndpointAddress epaddr = new EndpointAddress(uri, epid); ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(epaddr);
接受并投票,如果有帮助,否则返回查询
--RDB
Accept and vote if helps otherwise revert back with queries
--RDB
配置 SoapHeader 到webmethods将解决您的问题。 />
在webservices中,消息将通过网络转换为SOAP格式。
SOAP基本上包含SOAP Envelope,它是
$ b $的集合b SOAP标头(敏感且需要传输的数据通常是凭据,信用卡详细信息等)
SOAP Body (主要结果)
SOAP错误或错误(来自服务的错误消息)
定义一个实现的类 SoapHeader 并定义2个名为UserName和Password的属性。
ex:
Configuring SoapHeader to webmethods will solve your problem.
In webservices messages will be transformed through wire in SOAP format.
SOAP basically contains SOAP Envelope which is collection of
SOAP Header (The data which is sensitive and need to be transferred usually credentials, Credit card details.... etc )
SOAP Body (Main result)
SOAP Fault or Error (Error message from service)
Define a class which implements SoapHeader and define 2 properties named UserName and Password.
ex:
public class CustomAuth: System.Web.Services.Protocols.SoapHeader
{
public string UserName;
public string Password;
}
设置Soap Profile 1.1以启用此安全功能
以下是服务实现
Set the Soap Profile 1.1 to enable this security feature
Following is the service implementation
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestService : System.Web.Services.WebService
{
public CustomAuth myTestSvcAuthentication;
[WebMethod()]
[SoapHeader("myTestSvcAuthentication",Required=true)]
public string DoSomething()
{
string _username = myTestSvcAuthentication.UserName;
string _password = myTestSvcAuthentication.Password;
if (CheckCredentials(_username, _password))
{
// Write your logic for DoSomething method
return "Successfully done";
}
else
{
// return authentication failed message
return new Exception("Authentication failed");
}
}
private bool CheckCredentials(string uname,string pwd)
{
try
{
// your database connection or active directory repository
// login verification will be done here and return true
// if credentials are correct if not return false
}
catch(Exception ex)
{
//log and throw the error
}
}
}
Clie代码
Client Code
//add service reference in using section
using MyService;// as per your configuration
TestService svc = new TestService();
CustomAuth customAuth= new CustomAuth();
//Set your username and password as you made contract with Service owners
customAuth.UserName = "Yoganand";
customAuth.Password = "Yoga@123";
svc.myTestSvcAuthentication = customAuth;
Console.WriteLine(svc.DoSomething());
对于你的ref: http://msdn.microsoft.com/en-us/library/8728chd5(v = VS.80)的.aspx 一> [ ^ ]
希望它能帮到你.. :)
Happy Coding
For your ref: http://msdn.microsoft.com/en-us/library/8728chd5(v=vs.80).aspx[^]
hope it will help you.. :)
Happy Coding
这篇关于具有密码保护的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!