简体   繁体   English

WCF身份验证+授权

[英]WCF authentication + authorization

We have WCF 4.5 service and we are trying to secure it. 我们有WCF 4.5服务,我们正在努力确保其安全。

We have users in our database schema but what we want to do is operations like 我们在数据库架构中有用户,但我们要做的是像

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

our service is secured with basicHttpsBinding with basic authorization. 我们的服务由具有基本授权的basicHttpsBinding保护。 I can get username of user who accessed that method with: 我可以通过以下方式获取访问该方法的用户的用户名:

ServiceSecurityContext.Current.PrimaryIdentity.Name

But how can i Add his database Id into PrimaryIdentity. 但是我如何才能将他的数据库ID添加到PrimaryIdentity。 In other words, How to verify, that he user with username "John" can only send his userId. 换句话说,如何验证他的用户名“ John”的用户只能发送他的userId。 What is the best technology to verify him? 验证他的最佳技术是什么?

Also how it is possible that STATIC variable (Current) is different for each request. 同样,对于每个请求,STATIC变量(当前)如何可能不同。 We use default WCF instanceMode which is... PerSession. 我们使用默认的WCF instanceMode,即PerSession。 So how it is possible to have static variable different for each request. 因此,如何使每个请求的静态变量都不同。

It feels pretty lame to query database for userId by his username for each request. 通过每个请求的用户名查询数据库以查询userId感觉很la脚。

It feels pretty lame to query database for userId by his username for each request. 通过每个请求的用户名查询数据库以查询userId感觉很la脚。

I disagree. 我不同意。

In order to provide secure WCF service I would check credentials per request. 为了提供安全的WCF服务,我将根据请求检查凭据。 One way of doing it is to create custom UserNamePasswordValidator and override Validate method. 一种方法是创建自定义UserNamePasswordValidator并覆盖Validate方法。 You should also configure your Web.config to use your custom validator. 您还应该配置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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM