简体   繁体   English

验证后读取 WCF 服务中的用户名

[英]Read username in WCF service after authenticate

I have a custom validator that validate the incoming username and password in a webservice.我有一个自定义验证器,用于验证 Web 服务中传入的用户名和密码。 Once the validation is done, i need to use that user name and password inside the webservice.验证完成后,我需要在 web 服务中使用该用户名和密码。 Here is my CustomValidator这是我的 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");
           
            }

        }
    }

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 
        }

We could use the ServiceSecurityContext to obtain the username value, while we could not get the password after the credential is authenticated to pass.我们可以使用ServiceSecurityContext来获取用户名值,而在凭证通过认证后我们无法获取密码。

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()}";
}

Result.结果。
在此处输入图像描述
The security token based on the SAML, we only can obtain the claim sets.基于 SAML 的安全令牌,我们只能获取声明集。 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/framework/wcf/how-to-examine-the-security-context
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.servicesecuritycontext?view=netframework-4.8 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.如果有什么我可以帮忙的,请随时告诉我。

Came up with a different approach.想出了一个不同的方法。
Create a static class that can hold the user name and password from validator and use that in the webservice创建一个 static class 可以保存来自验证器的用户名和密码并在 web 服务中使用

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

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

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

        AuthUser.Username = userName;

        AuthUser.Password = password;

    }
}

public static class AuthUser
{
    private static string name = "";

    public static string Username
    {
        get { return name; }
        set { name = value; }

    }
    private static string pswrd = "";

    public static string Password
    {
        get { return pswrd; }
        set { pswrd = value; }

    }
}

In web service在 web 服务中

string username = AuthUser.Username;
  string passwod = AuthUser.Password;

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

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