验证后在WCF服务中读取用户名 [英] Read username in WCF service after authenticate

查看:78
本文介绍了验证后在WCF服务中读取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义验证器,用于验证Web服务中传入的用户名和密码. 验证完成后,我需要在webservice中使用该用户名和密码. 这是我的CustomValidator

I have a custom validator that validate the incoming username and password in a webservice . Once the validation is done, i need to use that user name and password inside the webservice . Here is my CustomValidator

  public class ServiceAuthenticator : UserNamePasswordValidator
    {
        private static readonly ILog _log = LogManager.GetLogger("ServiceAuthenticator");
        public override void Validate(String userName, string password)
        {

            _log.InfoFormat("-------------{0}/{1}------------------------------", userName, password);

           if (userName == null || password == null)
            {
                _log.WarnFormat("  Missing User-name / Password  {0}/{1}", userName, password);
                throw new FaultException("Incorrect User name or Password");
           
            }

        }
    }

现在我有一个Web服务,正在尝试获取上述用户名和密码

Now i have a webservice where i am trying to get the above user name and password

    [WebInvoke(Method = "POST", UriTemplate = "Uplooc")]
    [WebMethod(Description = "Save documents ")]
     public  void UploadDocGen(RemoteFileInfo remoteFileInfo)
        {
           // string UserName = ""; --- How i get the username
           // sting Password  = "";  -- How to get the password into this 
        }

推荐答案

我们可以使用 ServiceSecurityContext 来获取用户名值,而在认证通过后我们无法获取密码.

We could use the ServiceSecurityContext to obtain the username value, while we could not get the password after the credential is authenticated to pass.

public string SayHello()
{
    OperationContext oc = OperationContext.Current;
    var username1=oc.ServiceSecurityContext.PrimaryIdentity.Name;
    Console.WriteLine(username1);
    var username2 = ServiceSecurityContext.Current.PrimaryIdentity.Name;
    Console.WriteLine(username2);
    return $"Hello Buddy,{DateTime.Now.ToLongTimeString()}";
}

结果.

基于SAML的安全令牌,我们只能获取索赔集.这是一个复杂的主题,我不太了解. 以下是一些相关文档,希望对您有用.
https://docs. microsoft.com/en-us/dotnet/framework/wcf/how-to-examine-the-security-context
https://docs.microsoft. com/en-us/dotnet/api/system.servicemodel.servicesecuritycontext?view = netframework-4.8
请随时告诉我是否有什么我可以帮助的.

Result.

The security token based on the SAML, we only can obtain the claim sets. It is a complex topic, which I don’t know much. Here are some related documents, wish it is useful to you.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/how-to-examine-the-security-context
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.servicesecuritycontext?view=netframework-4.8
Feel free to let me know if there is anything I can help with.  

这篇关于验证后在WCF服务中读取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆