コード例 #1
0
        public CreateAttemptTokenResponse CreateAttemptToken(CreateAttemptTokenRequest request)
        {
            CreateAttemptTokenResponse response = new CreateAttemptTokenResponse();

            try
            {
                Common.Helpers.ValidationHelper.ValidateRequiredField(request.Token, "Token");

                InterviewToken token = InterviewToken.FromBytes(EncryptionHelper.DecryptURL(Convert.FromBase64String(request.Token)));

                DbContext context = DataController.CreateDbContext();

                long randomizer = Common.Helpers.RandomHelper.RandomLong();

                Guid mostRecentAttemptId = context.Attempts
                    .Where(a => a.InterviewQuestionID == request.QuestionID)
                    .OrderByDescending(a => a.CreatedDate)
                    .Select(a => a.ID)
                    .FirstOrDefault();

                AttemptToken attemptToken = new AttemptToken(request.QuestionID, randomizer, mostRecentAttemptId);
                response.AttemptToken = Convert.ToBase64String(EncryptionHelper.EncryptToken(attemptToken.AsBytes()));
            }
            catch (AuthenticationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Common.Exceptions.ValidationException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                ExceptionHelper.Log(ex, null);
                throw new WebFaultException<string>("An unknown error has occurred.", System.Net.HttpStatusCode.InternalServerError);
            }

            return response;
        }