Пример #1
0
        private void PerformRollCheck()
        {
            //var targetItem = VmDisplayObjects.FirstOrDefault(i => i.UserName == MySelectedObject.UserName);

            // Make this an async task
            // test connections
            // also consider trying fqdn and an ip address resolution test
            // check network connectivity
            // write a log to the users view
            // communicate to host that a new connection is being made.
            //return; // remove this when ready.
            //using (Process p = new Process())
            //{
            //    p.StartInfo.FileName = @"C:\WINDOWS\system32\mstsc.exe";
            //    p.StartInfo.Arguments = $"/v:{MySelectedObject.IpAddress}";
            //    p.StartInfo.RedirectStandardOutput = true;
            //    p.StartInfo.UseShellExecute = false;
            //    p.StartInfo.CreateNoWindow = true;
            //    p.Start();
            //    var x = p.Id;   // We need this to keep track of all the connections the user is maintaining. Keep track of these at a higher level and when one is removed, update host.
            //}

            // TODO: Proceed only if the rdc was successful!

            var timestampNow = DateTime.Now;

            RemoteUserAccount userConnection = new RemoteUserAccount()
            {
                username           = MySelectedObject.UserName ?? string.Empty,
                sitename           = MySelectedObject.SiteName ?? string.Empty,
                machinename        = MySelectedObject.MachineName ?? string.Empty,
                machinedescription = MySelectedObject.MachineDescription ?? string.Empty,
                domainname         = MySelectedObject.DomainName ?? string.Empty,
                ipaddress          = MySelectedObject.IpAddress ?? string.Empty,
                lastloginuser      = MySelectedObject.LastLoginUser ?? string.Empty,
                lastlogintimestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"),
                lastlogout         = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz") ?? string.Empty,
                lastloginip        = NetConfig.LocalIp ?? string.Empty,
                isavailable        = "NO"
            };

            var restData = JsonConvert.SerializeObject(userConnection);

            // Tell the host that we have connected to this user account.
            MyConnections.UpdateConnection(NetConfig.HostTargetUrlFq, restData);

            // Ensure update worked -- TODO: add exception handling here and maybe some retry logic.
        }
Пример #2
0
    public async Task WhenConnectingUserIsNotAuthenticated_UserDoesNotHaveAnyRole()
    {
        var remoteAccount = new RemoteUserAccount
        {
            AdditionalProperties = new Dictionary <string, object>()
        };
        var accessToken = new AccessTokenResult(AccessTokenResultStatus.Success, new AccessToken(), "");
        var sut         = CreateSut(out var tokenProvider, out _, out _);

        tokenProvider.TokenProvider.RequestAccessToken().Returns(accessToken);

        var claims = await sut.CreateUserAsync(remoteAccount, new RemoteAuthenticationUserOptions()).ConfigureAwait(true);

        claims.IsInRole(Role.Guest.ToString()).Should().BeFalse();
        claims.IsInRole(Role.User.ToString()).Should().BeFalse();
    }
Пример #3
0
    public async Task WhenGetConnectReturnsAccountButDeserializationFails_UserIsTreatedAsGuest()
    {
        var remoteAccount = new RemoteUserAccount
        {
            AdditionalProperties = new Dictionary <string, object>()
        };
        var accessToken = new AccessTokenResult(AccessTokenResultStatus.Success, new AccessToken(), "");
        var options     = new RemoteAuthenticationUserOptions {
            AuthenticationType = "Basic"
        };
        var sut = CreateSut(out var tokenProvider, out var mockHttp, out var logger);

        tokenProvider.TokenProvider.RequestAccessToken().Returns(accessToken);
        mockHttp.When(HttpMethod.Get, "http://localhost/api/user/connect").Respond("application/json", "{ baz }");

        var claims = await sut.CreateUserAsync(remoteAccount, options).ConfigureAwait(true);

        claims.IsInRole(Role.Guest.ToString()).Should().BeTrue();
        claims.IsInRole(Role.User.ToString()).Should().BeFalse();
    }
Пример #4
0
    public async Task WhenGetConnectFails_UserIsTreatedAsGuest()
    {
        var remoteAccount = new RemoteUserAccount
        {
            AdditionalProperties = new Dictionary <string, object>()
        };
        var accessToken = new AccessTokenResult(AccessTokenResultStatus.Success, new AccessToken(), "");
        var options     = new RemoteAuthenticationUserOptions {
            AuthenticationType = "Basic"
        };
        var sut = CreateSut(out var tokenProvider, out _, out var logger);

        tokenProvider.TokenProvider.RequestAccessToken().Returns(accessToken);

        var claims = await sut.CreateUserAsync(remoteAccount, options).ConfigureAwait(true);

        claims.IsInRole(Role.Guest.ToString()).Should().BeTrue();
        claims.IsInRole(Role.User.ToString()).Should().BeFalse();
        logger.Received(1).LogError(Arg.Any <Exception>(), "Connect API request failed with status code: NotFound");
    }
    public async override ValueTask <ClaimsPrincipal> CreateUserAsync(RemoteUserAccount account, RemoteAuthenticationUserOptions options)
    {
        var initialUser = await base.CreateUserAsync(account, options);

        if (initialUser?.Identity?.IsAuthenticated ?? false)
        {
            var userIdentity = (ClaimsIdentity)initialUser.Identity;

            account.AdditionalProperties.TryGetValue(ClaimTypes.Role, out var roleClaimValue);

            if (roleClaimValue is not null && roleClaimValue is JsonElement element && element.ValueKind == JsonValueKind.Array)
            {
                userIdentity.RemoveClaim(userIdentity.FindFirst(ClaimTypes.Role));

                var claims = element.EnumerateArray()
                             .Select(x => new Claim(ClaimTypes.Role, x.ToString()));

                userIdentity.AddClaims(claims);
            }
        }

        return(initialUser ?? new ClaimsPrincipal());
    }