WCF身份验证+授权

时间:2015-02-20 12:24:11

标签: c# sql wcf authorization

我们有WCF 4.5服务,我们正在努力保护它。

我们的数据库架构中有用户,但我们想要做的是像

这样的操作
[OperationsContract]
void PostMessage(string message, int userId) 
   //used ID is supposed to be id of user who post message

我们的服务使用basicHttpsBinding和基本授权进行保护。 我可以通过以下方式获取访问该方法的用户的用户名:

ServiceSecurityContext.Current.PrimaryIdentity.Name

但是如何将他的数据库ID添加到PrimaryIdentity中。 换句话说,如何验证,用户使用用户名" John"只能发送他的userId。验证他的最佳技术是什么?

对于每个请求,STATIC变量(当前)的可能性如何。我们使用默认的WCF instanceMode,即... PerSession。那么如何为每个请求提供不同的静态变量。

通过每个请求的用户名为userId查询数据库感觉相当蹩脚。

1 个答案:

答案 0 :(得分:1)

  

通过每个请求的用户名为userId查询数据库感觉相当蹩脚。

我不同意。

为了提供安全的WCF服务,我会检查每个请求的凭据。一种方法是创建自定义UserNamePasswordValidator并覆盖Validate方法。您还应该将Web.config配置为使用自定义验证程序。

<behaviors>
  <serviceBehaviors>
    <behavior name="GeneralBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WCFApp.UserNamePassValidator, WCFApp" />
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>