Example #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            bool   validKey = false;
            string apiKey   = EncryptDecryptPassword.EncryptPlainTextToCipherText(Convert.ToString(ConfigurationSettings.AppSettings["ApiKey"]));
            IEnumerable <string> requestHeaders;
            var checkApiKeyExists = request.Headers.TryGetValues("ApiKey", out requestHeaders);

            if (checkApiKeyExists)
            {
                if (requestHeaders.FirstOrDefault().Equals(apiKey))
                {
                    validKey = true;
                }
            }
            if (!validKey)
            {
                return(new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(new { message = "Invalid API Key" })),
                    ReasonPhrase = "Invalid API Key"
                });
            }
            var response = await base.SendAsync(request, cancellationToken);

            return(response);
        }
Example #2
0
        public HttpResponseMessage Registration(UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
            }
            else
            {
                var encryptedPassword = EncryptDecryptPassword.EncryptPlainTextToCipherText(model.Password);

                db.UserTbls.Add(new UserTbl()
                {
                    CityId         = model.CityId,
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    Email          = model.Email,
                    MobileNumber   = model.MobileNumber,
                    FlatNumber     = model.FlatNumber,
                    CountryId      = model.CountryId,
                    StateId        = model.StateId,
                    SocityName     = model.SocityName,
                    DateModified   = DateTime.Now,
                    DateAdded      = DateTime.Now,
                    WhatsAppNumber = model.WhatsAppNumber,
                    WingName       = model.WingName,
                    Landmark       = model.Landmark,
                    Password       = encryptedPassword,
                    UserName       = model.UserName,
                    Token          = model.Token
                });
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
        }
Example #3
0
 public HttpResponseMessage Login(UserTbl model)
 {
     if (ModelState.IsValid)
     {
         var encryptedPasswordString = EncryptDecryptPassword.EncryptPlainTextToCipherText(model.Password);
         var user = db.UserTbls.FirstOrDefault(item => item.UserName == model.UserName && item.Password == encryptedPasswordString);
         if (user != null)
         {
             user.Token = Guid.NewGuid();
             db.SaveChanges();
             return(Request.CreateResponse(HttpStatusCode.OK, new
             {
                 Token = user.Token,
                 Name = string.Concat(user.FirstName, " ", user.LastName)
             }));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Username and password"));
         }
     }
     else
     {
         return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }