using System;
using System.Runtime.InteropServices;
using Lithnet.CredentialProvider.Interop;
namespace Lithnet.CredentialProvider
{
///
/// Represents a user provided by the credential framework
///
public class CredentialProviderUser
{
internal readonly ICredentialProviderUser User;
private readonly ILogger logger;
private string qualifiedUserName;
private string sid;
private string userName;
private string displayName;
private string logonStatus;
private string providerId;
internal CredentialProviderUser(ILoggerFactory loggerFactory, ICredentialProviderUser user)
{
this.User = user;
this.logger = loggerFactory.CreateLogger();
}
///
/// Gets the qualified username of the user. This name is used to pack an authentication buffer
///
public string QualifiedUserName
{
get
{
if (this.qualifiedUserName == null)
{
this.qualifiedUserName = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_QualifiedUserName);
}
return this.qualifiedUserName;
}
}
///
/// Gets the SID of the user
///
public string Sid
{
get
{
if (this.sid == null)
{
this.sid = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_PrimarySid);
}
return this.sid;
}
}
///
/// Gets the username of the user
///
public string UserName
{
get
{
if (this.userName == null)
{
this.userName = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_UserName);
}
return this.userName;
}
}
///
/// Gets the display name of the user
///
public string DisplayName
{
get
{
if (this.displayName == null)
{
this.displayName = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_DisplayName);
}
return this.displayName;
}
}
///
/// Gets the logon status of the user. Does not apply to the CredUI scenario.
///
/// For example, "Signed-in", "Locked"
public string LogonStatus
{
get
{
if (this.logonStatus == null)
{
this.logonStatus = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_LogonStatusString);
}
return this.logonStatus;
}
}
///
/// The user's provider ID
///
public string ProviderID
{
get
{
if (this.providerId == null)
{
this.providerId = this.TryGetValueOrDefault(PropertyKeys.PKEY_Identity_ProviderID);
}
return this.providerId;
}
}
private string GetValue(PropertyKey key)
{
var result = this.User.GetStringValue(key, out var value);
if (result != HRESULT.S_OK)
{
throw new COMException("Could not get user SID", result);
}
return value;
}
private string TryGetValueOrDefault(PropertyKey key)
{
try
{
return this.GetValue(key);
}
catch (Exception ex)
{
this.logger.LogError(ex, $"Unable to get property value for key {key.PropertyID}");
}
return null;
}
}
}