【问题标题】:Using username and password to authenticate wcf service users使用用户名和密码对 wcf 服务用户进行身份验证
【发布时间】:2014-03-08 21:08:57
【问题描述】:

我已将 WCF 服务添加到托管在 IIS 上的现有 ASP.NET 网站。该服务的用户是我的注册用户,他们的凭据存储在我的 SQL Server 数据库中。在通过 WCF 服务提供数据之前,我只希望用户名和密码对有效用户进行身份验证。我在网上浏览了很多链接和文本,但除了增加我的困惑之外,我无法掌握关于身份验证和授权的内容。我在初始阶段没有使用 HTTPS 和证书。

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

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

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

非常感谢!

【问题讨论】:

  • 转到 WCF REST。很简单。
  • 您好 Faizan,谢谢!我正在使用 WCF REST。您能否解释一下如何传递用户名密码并在服务中检索相同的密码。
  • 感谢费赞的宝贵意见!我已经使用客户端 html 文件中的 Ajax/JQuery 在 http 请求中成功添加了 Authorization 标头,并且还在 wcf 端检索了用户凭据。现在又想到一个问题,wcf 端是否有任何方法可以知道用户是否没有提供授权标头并自动发送 401,而不是进一步处理请求。

标签: wcf web-services authentication


【解决方案1】:

好的!这是 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

当您在浏览器上输入此网址并按 Enter 时,如果服务正在运行,它将调用您的“MyMethod”。

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

【讨论】:

  • 非常感谢法赞!我可以通过 http 标头类型传递用户名和密码吗?因为我有更多用于过滤数据的查询字符串参数。如果是,那么如何在服务中传递和检索相同的内容。谢谢。
  • 查看此设置标头msdn.microsoft.com/en-us/library/gg258441(v=vs.110).aspx,您可以使用 IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest; 在 WCF 方法中获取标头WebHeaderCollection headers = request.Headers;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 2011-10-05
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
相关资源
最近更新 更多