简体   繁体   中英

Read username in WCF service after authenticate

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

        }
    }

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.

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. 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.

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

 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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