【问题标题】:Obtaining ClientCredentials from WCF operation从 WCF 操作获取 ClientCredentials
【发布时间】:2009-09-01 21:36:49
【问题描述】:

我的 WCF 服务使用自定义凭据验证器来实现基于消息的自定义安全性,因为我想确保在我的 Web 服务上调用操作的每个客户端在我的数据库中都有相应的用户名和密码。

Imports System.IdentityModel.Selectors
Imports System.IdentityModel.Tokens

Public Class CredentialsValidator
    Inherits UserNamePasswordValidator

    Public Overrides Sub Validate(ByVal userName As String, ByVal password As String)
        Dim authenticationApi As New AuthenticationGateway()
        If Not authenticationApi.IsValid(userName, password) Then
            Throw New SecurityTokenException("Validation Failed.")
        End If
    End Sub
End Class

问题是,一旦用户通过身份验证,我想在我的 OperationContract 实现中使用 userName 来进行基于上下文的调用。

<ServiceContract(Name:="IMyService")> _
Public Interface IMyService
    <OperationContract()> _
    Function GetAccountNumber() As String
End Interface

Public Class IntegrationService
    Implements IIntegrationService

    Public Function GetAccountNumber() As String Implements IMyService.GetAccountNumber
        Dim userName As String '' Here I want the userName set from credentials
        Dim accountApi As New AccountGateway()
        Return accountApi.GetAccountNumber(userName)
    End Function
End Class

我不能依赖被调用者的诚实来指定他们的实际用户名,所以如果不传递密码就无法传递它。我想避免每次调用我的 Web 服务时都必须接受 ClientCredentials。

谢谢。

【问题讨论】:

    标签: wcf wcf-security wcf-extensions


    【解决方案1】:
    Public Class IntegrationService
        Implements IIntegrationService
    
        Private ReadOnly Property UserName As String
            Get
                Return ServiceSecurityContext.Current.PrimaryIdentity.Name
            End Get
        End Property
    
        Public Function GetAccountNumber() As String Implements IMyService.GetAccountNumber
            Dim accountApi As New AccountGateway()
            Return accountApi.GetAccountNumber(UserName)
        End Function
    End Class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 2014-06-30
      • 2011-08-23
      相关资源
      最近更新 更多