我想创建wcf web service
有username
和password
在asp.net
.so通过提供该用户名和密码,我们可以使用任何Web方法。我已经创建了wcf Web服务,但我想在Web服务中添加用户名和密码。如何在asp.net中创建具有用户名和密码的wcf Web服务?
在此先感谢。
我想创建wcf web service
有username
和password
在asp.net
.so通过提供该用户名和密码,我们可以使用任何Web方法。我已经创建了wcf Web服务,但我想在Web服务中添加用户名和密码。如何在asp.net中创建具有用户名和密码的wcf Web服务?
在此先感谢。
下面是必要的步骤,以达到你的要求为:
1)使用Visual Studio的“新项目”界面中创建一个新的WCF项目:你有两个主要文件结束:服务1 .svc(代码文件)和IService1.cs(接口文件)。
2)打开IService1.cs文件并定义类似下面的你的方法:
[ServiceContract]
public interface IService1
{
[...]
// TODO: Add your service operations here
[OperationContract]
string GetToken(string userName, string password);
}
3)打开Service1.cs文件,并添加方法的实施方式如下:
/// <summary>
/// Retrieve a non-permanent token to be used for any subsequent WS call.
/// </summary>
/// <param name="userName">a valid userName</param>
/// <param name="password">the corresponding password</param>
/// <returns>a GUID if authentication succeeds, or string.Empty if something goes wrong</returns>
public string GetToken(string userName, string password)
{
// TODO: replace the following sample with an actual auth logic
if (userName == "testUser" && password == "testPassword")
{
// Authentication Successful
return Guid.NewGuid().ToString();
}
else
{
// Authentication Failed
return string.Empty;
}
}
基本上就是这样。您可以使用此技术检索(即将到期的&安全)令牌以用于任何后续调用 - 这是最常见的行为 - 或者在您的所有方法中实施该用户名/密码策略:这完全是您的呼叫。
要测试新服务,您可以在调试模式启动你的MVC项目,并使用内置WCF测试工具:你必须输入上面(为testUser,testPassword规定的采样值),除非你改变它们。
有关此特定主题和附加实施示例的更多信息,您还可以read this post。
所以基本上,你想验证请求到你的服务。 [看看这个](http://stackoverflow.com/questions/4949792/wcf-service-using-asp-net-forms-authentication)和谷歌更多 – 2013-04-04 06:29:04
但如何设置用户名和密码?比如uname =“abc”和password =“123”,以及如何验证它是对还是错? – Naresh 2013-04-04 06:37:10
创建一个WCF服务来验证用户身份,为用户创建一个安全令牌,然后将安全性传递给您的方法,然后进行验证。 – Sajeetharan 2013-04-05 08:39:38