Exemple #1
0
        public async Task <IActionResult> LoginVoter(InVoterLoginDTO inVoterLoginDto)
        {
            var user = await _userManager.FindByNameAsync(inVoterLoginDto.UserName);

            if (user == null)
            {
                return(BadRequest());
            }
            var userDetail = await _context.UserDetails.FirstOrDefaultAsync(x => x.UserId == user.Id);

            if (userDetail.CNP != inVoterLoginDto.CNP || userDetail.Series != inVoterLoginDto.Series ||
                userDetail.Number != inVoterLoginDto.Number)
            {
                return(Unauthorized());
            }


            var signInResult = await _signInManager.CheckPasswordSignInAsync(user, inVoterLoginDto.Password, false);

            if (!signInResult.Succeeded)
            {
                if (signInResult.IsNotAllowed)
                {
                    return(Unauthorized());
                }
            }

            string token = string.Empty;

            try
            {
                token = await _userManager.GenerateUserTokenAsync(user, "EVoting", "Vote");
            }
            catch (Exception e)
            {
            }


            var caPublicKey = _cAuthService.GetPublicKey();



            var outVoterLoginDto = new OutVoterLoginDTO()
            {
                UserId              = user.Id,
                CAuthPublicKey      = caPublicKey,
                Token               = token,
                EncryptedPrivateKey = userDetail.EncryptedPrivateKey,
                PublicKey           = userDetail.PublicKey
            };

            return(Ok(outVoterLoginDto));
        }
Exemple #2
0
        static void Vote(HttpClient client)
        {
            var votingService = new VotingService();

            Console.WriteLine("You are voting now");

            Console.WriteLine("UserName: "******"Password");
            var password = Console.ReadLine();

            Console.WriteLine("CNP: ");
            var cnp = Console.ReadLine();

            Console.WriteLine("ID Series: ");
            var series = Console.ReadLine();

            Console.WriteLine("ID Number: ");
            var number = Console.ReadLine();

            var inVoterLoginDto = new InVoterLoginDTO()
            {
                UserName = username,
                CNP      = cnp,
                Number   = number,
                Series   = series,
                Password = password
            };

            var res = client.PostAsync("https://localhost:44355/api/auth/login-voter",
                                       new StringContent(JsonSerializer.Serialize(inVoterLoginDto, typeof(InVoterLoginDTO)),
                                                         Encoding.UTF8, "application/json"))
                      .Result;

            var credentials = JsonSerializer.Deserialize <OutVoterLoginDTO>(res.Content.ReadAsStringAsync().Result, new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            });


            var res2       = client.GetStringAsync("https://localhost:44355/api/voting/candidates").Result;
            var candidates = JsonSerializer.Deserialize <BaseResponseDTO <List <CandidateDTO> > >(res2, new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            }).Result;


            int i = 0;

            foreach (var candidate in candidates)
            {
                Console.WriteLine($"Option {i}, {candidate.Name}, {candidate.Party}\n\n");
                i++;
            }

            Console.WriteLine("\n\nYour option: ");
            var option            = int.Parse(Console.ReadLine());
            var candidateSelected = candidates[option];
            var candidateId       = candidateSelected.Id;


            votingService.InitialiseKeys(credentials.EncryptedPrivateKey, credentials.PublicKey, password);
            var userSignedVote = votingService.CastVote(credentials.UserId, candidateId, credentials.CAuthPublicKey);

            userSignedVote.Token    = credentials.Token;
            userSignedVote.UserName = username;

            var res3 = client.PostAsync("https://localhost:44355/api/voting/cast-vote", new StringContent(
                                            JsonSerializer.Serialize(userSignedVote, typeof(UserSignedVoteDTO)), Encoding.UTF8,
                                            "application/json"))
                       .Result;

            var response = res3.Content.ReadAsStringAsync().Result;

            Console.WriteLine(response);
        }