本文整理汇总了C#中System.Security.Principal.WindowsPrincipal.AddIdentities方法的典型用法代码示例。如果您正苦于以下问题:C# WindowsPrincipal.AddIdentities方法的具体用法?C# WindowsPrincipal.AddIdentities怎么用?C# WindowsPrincipal.AddIdentities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Principal.WindowsPrincipal
的用法示例。
在下文中一共展示了WindowsPrincipal.AddIdentities方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SignInUsingLogonAsync
private async Task<ClaimsPrincipal> SignInUsingLogonAsync(BasicSignInContext context) {
var user = new StringBuilder(NativeMethods.CREDUI_MAX_USERNAME_LENGTH + 1);
var domain = new StringBuilder(NativeMethods.CREDUI_MAX_PASSWORD_LENGTH + 1);
uint error = NativeMethods.CredUIParseUserName(context.Username, user, user.Capacity, domain, domain.Capacity);
if (error != 0) {
_logger.LogError(Resources.Error_UserNameParse, context.Username, error.ToString("X"));
return null;
}
IntPtr token;
WindowsIdentity winIdentity = null;
string profilePath = string.Empty;
_logger.LogTrace(Resources.Trace_LogOnUserBegin, context.Username);
if (NativeMethods.LogonUser(user.ToString(), domain.ToString(), context.Password, (int)LogonType.LOGON32_LOGON_NETWORK, (int)LogonProvider.LOGON32_PROVIDER_DEFAULT, out token)) {
_logger.LogTrace(Resources.Trace_LogOnSuccess, context.Username);
winIdentity = new WindowsIdentity(token);
StringBuilder profileDir = new StringBuilder(NativeMethods.MAX_PATH * 2);
uint size = (uint)profileDir.Capacity;
if (NativeMethods.GetUserProfileDirectory(token, profileDir, ref size)) {
profilePath = profileDir.ToString();
_logger.LogTrace(Resources.Trace_UserProfileDirectory, context.Username, profilePath);
} else {
#if DEBUG
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMinutes(10));
#else
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
#endif
_logger.LogTrace(Resources.Trace_UserProfileCreation, context.Username);
var result = await _userProfileManager.CreateProfileAsync(new RUserProfileServiceRequest(user.ToString(), domain.ToString(), context.Password.ToSecureString()), cts.Token);
if (result.IsInvalidResponse()) {
_logger.LogError(Resources.Error_ProfileCreationFailedInvalidResponse, context.Username, Resources.Info_UserProfileServiceName);
return null;
}
error = result.Error;
// 0x800700b7 - Profile already exists.
if (error != 0 && error != 0x800700b7) {
_logger.LogError(Resources.Error_ProfileCreationFailed, context.Username, error.ToString("X"));
return null;
} else if (error == 0x800700b7 || result.ProfileExists) {
_logger.LogInformation(Resources.Info_ProfileAlreadyExists, context.Username);
} else {
_logger.LogInformation(Resources.Info_ProfileCreated, context.Username);
}
if (!string.IsNullOrEmpty(result.ProfilePath)) {
profilePath = result.ProfilePath;
_logger.LogTrace(Resources.Trace_UserProfileDirectory, context.Username, profilePath);
} else {
_logger.LogError(Resources.Error_GetUserProfileDirectory, context.Username, Marshal.GetLastWin32Error().ToString("X"));
}
}
} else {
_logger.LogError(Resources.Error_LogOnFailed, context.Username, Marshal.GetLastWin32Error().ToString("X"));
return null;
}
var principal = new WindowsPrincipal(winIdentity);
if (principal.IsInRole(_options.AllowedGroup)) {
var claims = new[] {
//new Claim(ClaimTypes.Name, context.Username),
new Claim(Claims.RUser, ""),
// TODO: figure out how to avoid keeping raw credentials around.
new Claim(Claims.Password, context.Password),
new Claim(Claims.RUserProfileDir, profilePath)
};
var claimsIdentity = new ClaimsIdentity(claims, context.Options.AuthenticationScheme);
principal.AddIdentities(new[] { claimsIdentity });
}
return principal;
}