2014-02-11 157 views
0

我已经将WCF服务添加到IIS上托管的现有ASP.NET网站。此服务的用户是我的注册用户,他们的凭据存储在我的SQL Server数据库中。我只是希望在通过WCF服务提供数据之前用户名和密码来验证有效的用户。我经历了网络上的大量链接和文字,但我无法掌握认证和授权中给出的内容,除非增加我的疑惑。在初始阶段,我没有使用HTTPS和证书。使用用户名和密码验证wcf服务用户

我想要一个简单的解决方案,服务的用户在每次调用API时都会传递用户名和密码。我将收到这些凭证,根据我的SQL服务器数据库进行验证并相应地提供XML/JSON数据。 API调用将以AJAX/JQuery的形式从浏览器或移动客户端获得。如果可能,我希望客户端通过HTTP标头中的凭据而不是查询字符串,并且在服务器端从HTTP标头中检索用户凭据。

任何示例的简单方法将不胜感激。

请注意,我不是.NET开发或Web服务方面的专家。

非常感谢!

+0

转到WCF REST。这很容易。 –

+0

嗨Faizan谢谢!我正在使用WCF REST。你能解释一下如何传递用户名密码并检索服务中的相同内容。 – RamKr

+0

感谢您的宝贵意见!我已经成功地在http请求中使用来自客户端html文件的Ajax/JQuery添加了Authorization标头,并且还检索了wcf端的用户凭证。现在还有一个问题想到,wcf方面有没有什么方法可以知道用户是否没有提供授权头并自动发送401,而不是进一步处理请求。 – RamKr

回答

0

好的!这是GET方法,尽管强烈建议不要在查询字符串中发送用户名和密码,但要为您提供启动器,您可以使用它。

定义你的合同:

[ServiceContract] 
public interface IMyService 
{   
    [OperationContract] 
    [WebGet(UriTemplate = "/username={username}&password={password}", 
     ResponseFormat = WebMessageFormat.XML, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    string MyMethod(string username, string password);   
} 

而在你的实现方法,可以检索相同的用户名和密码。

public class MyServiceImpl:IMyService 
{ 
    public string MyMethod(string username, string password) 
    { 
     return "You entered "+username +" and "+password; // This will returned as an XML. 
    } 
} 

更改的Web.Config像:

<?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" maxRequestLength="2147483647" executionTimeout="300"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="" name="Namespace.MyService"> 
     <endpoint address="" behaviorConfiguration="RESTBehavior" binding="webHttpBinding" 
      bindingConfiguration="webHttpBG" contract="Namespace.Name.IMyService" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:51855/MyService.svc" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
     <bindings> 
      <webHttpBinding> 
       <binding name="webHttpBG" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" 
    maxBufferPoolSize="21474836470" closeTimeout="00:01:00" openTimeout="00:01:00" 
       receiveTimeout="00:10:00" sendTimeout="00:01:00"> 
        <security mode="None"/> 
       </binding>     
     </webHttpBinding> 
    </bindings> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="RESTBehavior"> 
     <webHttp helpEnabled="true"/> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior> 
     <!-- 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> 
</behaviors> 
<protocolMapping> 
    <add binding="webHttpBinding" scheme="http" bindingConfiguration="webHttpBG" /> 
</protocolMapping>  
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 

并进行测试,您可以使用浏览器。在Web.Config文件中定义类型url。例如

http://localhost:51855/MyService.svc/username=myname&password=123 

当你键入浏览器这个网址并回车就会打电话给你“的MyMethod”提供服务正在运行。

您也可以使用Fiddler来测试您的服务。

+0

非常感谢Faizan!我可以通过http标头种类来传递用户名和密码,因为我有更多的查询字符串参数用于过滤数据。如果是,那么如何通过并检索相同的服务。谢谢。 – RamKr

+0

看到这个设置标头http://msdn.microsoft.com/en-us/library/gg258441(v=vs.110).aspx,你可以在WCF方法使用IncomingWebRequestContext请求= WebOperationContext.Current.IncomingRequest; WebHeaderCollection headers = request.Headers; –