public HttpResponseMessage AuthenticateUser(UserProfileToken objUser)
        {
            ResponseTokenWithEmailId respToken = new ResponseTokenWithEmailId();

            try
            {
                UserToken userProfile = new UserToken();
                userProfile.EmailId = objUser.EmailId;

                var rpostServiceAPI = new RpostServiceAPI();

                using (var dbcontext = new eSignEntities())
                {
                    UserTokenRepository tokenRepository = new UserTokenRepository(dbcontext);
                    eSign.Notification.RpostService.AuthorizationResponse response = rpostServiceAPI.AuthenticateUser(objUser.EmailId, objUser.Password);

                    if (!string.IsNullOrEmpty(response.AuthorizationKey))
                    {
                        UserToken objToken = new UserToken();

                        objToken.ID          = tokenRepository.GetUserProfileIDByEmail(objUser.EmailId);
                        objToken.EmailId     = objUser.EmailId;
                        objToken.AuthToken   = response.AuthorizationKey;
                        objToken.LastUpdated = DateTime.Now;
                        objToken.ExpiresIn   = DateTime.Now.AddDays(Convert.ToDouble(ConfigurationManager.AppSettings["ExpiryDays"]));

                        bool result = tokenRepository.Save(objToken);
                        dbcontext.SaveChanges();

                        respToken.AuthMessage = Convert.ToString(ConfigurationManager.AppSettings["TokenSuccess"].ToString());
                        respToken.AuthToken   = response.AuthorizationKey;
                        respToken.EmailId     = objUser.EmailId;
                        HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.OK, respToken);
                        return(responseToClient);
                    }
                    else
                    {
                        ResponseMessageWithEmailId responseMessage = new ResponseMessageWithEmailId();
                        responseMessage.StatusCode    = HttpStatusCode.NotAcceptable;
                        responseMessage.StatusMessage = "NotAcceptable";
                        responseMessage.Message       = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["InvalidUser"].ToString());
                        responseMessage.EmailId       = objUser.EmailId;
                        HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.NotAcceptable, responseMessage);
                        return(responseToClient);
                    }
                }
            }
            catch (Exception ex)
            {
                HttpResponseMessage responseToClient = Request.CreateResponse((HttpStatusCode)422);
                responseToClient.Content = new StringContent(ex.Message, Encoding.Unicode);
                throw new HttpResponseException(responseToClient);
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage RegisterUser(UserProfileToken objUser)
        {
            try
            {
                var rpostServiceAPI = new RpostServiceAPI();

                if (!EnvelopeHelper.IsEmailValid(objUser.EmailId))
                {
                    ResponseMessage responseMessage = new ResponseMessage();
                    responseMessage.StatusCode    = HttpStatusCode.Forbidden;
                    responseMessage.StatusMessage = "Forbidden";
                    responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["EmailWrong"].ToString());
                    HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.Forbidden, responseMessage);
                    return(responseToClient);
                }

                eSign.Notification.RpostService.ServiceResponse response = rpostServiceAPI.RegisterUser(objUser.EmailId, objUser.Password, "1", "en-US", objUser.FirstName + " " + objUser.LastName);



                if (response.Response == "Registration Successfully Processed")
                {
                    var userProfile = new UserProfile
                    {
                        ID        = Guid.NewGuid(),
                        UserID    = Guid.NewGuid(),
                        EmailID   = objUser.EmailId,
                        FirstName = objUser.FirstName,
                        LastName  = objUser.LastName
                    };

                    using (var dbContext = new eSignEntities())
                    {
                        UserRepository userRepository = new UserRepository(dbContext);
                        userRepository.Save(userProfile);
                        dbContext.SaveChanges();
                    }
                    ResponseMessage responseMessage = new ResponseMessage();
                    responseMessage.StatusCode    = HttpStatusCode.Created;
                    responseMessage.StatusMessage = "Created";
                    responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["Success"].ToString());
                    HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.Created, responseMessage);
                    return(responseToClient);
                }
                else if (response.Response == "The email address that entered is already registered in the system.")
                {
                    ResponseMessage responseMessage = new ResponseMessage();
                    responseMessage.StatusCode    = HttpStatusCode.Conflict;
                    responseMessage.StatusMessage = "Conflict";
                    responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["AlreadyRegistered"].ToString());
                    HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.Conflict, responseMessage);
                    return(responseToClient);
                }
                else
                {
                    ResponseMessage responseMessage = new ResponseMessage();
                    responseMessage.StatusCode    = HttpStatusCode.BadRequest;
                    responseMessage.StatusMessage = "BadRequest";
                    responseMessage.Message       = Convert.ToString(ConfigurationManager.AppSettings["Failed"].ToString());
                    HttpResponseMessage responseToClient = Request.CreateResponse(HttpStatusCode.BadRequest, responseMessage);
                    return(responseToClient);
                }
            }
            catch (Exception ex)
            {
                HttpResponseMessage responseToClient = Request.CreateResponse((HttpStatusCode)422);
                responseToClient.Content = new StringContent(ex.Message, Encoding.Unicode);
                throw new HttpResponseException(responseToClient);
            }
        }