当前位置: 首页>>代码示例>>C#>>正文


C# WindowsPrincipal.AddIdentities方法代码示例

本文整理汇总了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;
        }
开发者ID:Microsoft,项目名称:RTVS,代码行数:77,代码来源:SecurityManager.cs


注:本文中的System.Security.Principal.WindowsPrincipal.AddIdentities方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。