作者:mobiledu2502932307 | 来源:互联网 | 2023-10-11 17:46
我有一个自定义验证器,用于验证Webservice中传入的用户名和密码。
验证完成后,我需要在webservice中使用该用户名和密码。
这是我的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}",password);
throw new FaultException("Incorrect User name or Password");
}
}
}
现在我有一个Web服务,我正在尝试获取上述用户名和密码
[WebInvoke(Method = "POST",UriTemplate = "UploadDoc")]
[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
来获取用户名值,而在认证通过后我们无法获取密码。
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
请随时告诉我是否有什么我可以帮助的。
,
采用其他方法。
创建一个可以容纳验证器用户名和密码的静态类,并在网络服务中使用它
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}",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; }
}
}
在网络服务中
string username = AuthUser.Username;
string passwod = AuthUser.Password;