Ejemplo n.º 1
0
        public Task <JsonResponse> TurnOffTwoFactor(int UserID)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    try
                    {
                        User user = db.Users.Where(x => x.ID == UserID).SingleOrDefault();
                        if (user == null)
                        {
                            return new JsonResponse()
                            {
                                Flag = true, Message = "User is not found"
                            };
                        }
                        else
                        {
                            //bool flag = false;
                            string Message = "";
                            var aspNetUserID = db.AspNetUsers.Where(x => x.UserName == user.LoginName).Select(x => x.Id).SingleOrDefault();
                            TwoFactorAuthenticatedUser twoFactorUser = db.TwoFactorAuthenticatedUsers.Where(x => x.AspNetUserID == aspNetUserID).SingleOrDefault();

                            if (twoFactorUser != null)
                            {
                                db.TwoFactorAuthenticatedUsers.Remove(twoFactorUser);
                                db.SaveChanges();
                                Message = "Two-Factor Authentication is Off.";
                                //flag = true;
                            }
                            return new JsonResponse()
                            {
                                Flag = true, Message = Message
                            };
                        }
                    }
                    catch (Exception ex)
                    {
                        return new JsonResponse()
                        {
                            Flag = false, Message = ex.Message
                        };
                    }
                }
            }));
        }
Ejemplo n.º 2
0
        public Task <JsonResponse> TwoFactorVerify(string AspNetUserID, string Code)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    try
                    {
                        TwoFactorAuthenticatedUser user = db.TwoFactorAuthenticatedUsers.Where(x => x.AspNetUserID == AspNetUserID && x.Code == Code).SingleOrDefault();

                        if (user == null)
                        {
                            return new JsonResponse()
                            {
                                Flag = true, Message = "User is not found"
                            };
                        }
                        else
                        {
                            string Message = "Your code is expired.";
                            if (user.UpdatedDate.AddMinutes(2).ToUniversalTime() > DateTime.Now.ToUniversalTime())
                            {
                                user.IsVerified = true;
                                //db.TwoFactorAuthenticatedUsers.Add(user);
                                db.SaveChanges();
                                Message = "Two-Factor Authentication is On.";
                            }
                            return new JsonResponse()
                            {
                                Flag = true, Message = Message
                            };
                        }
                    }
                    catch (Exception ex)
                    {
                        return new JsonResponse()
                        {
                            Flag = false, Message = ex.Message
                        };
                    }
                }
            }));
        }
Ejemplo n.º 3
0
        public Task <JsonResponse> TwoFactorAuthentication(string email)
        {
            return(Task.Run(() =>
            {
                using (MIUEntities db = new MIUEntities())
                {
                    try
                    {
                        User user = db.Users.Where(x => x.EmailAccount == email).FirstOrDefault();
                        if (user == null)
                        {
                            return new JsonResponse()
                            {
                                Flag = true, Message = "User is not found"
                            };;
                        }
                        else
                        {
                            Random rnd = new Random();
                            string code = rnd.Next(1, 999999).ToString("D6");
                            AspNetUser aspNetUser = db.AspNetUsers.Where(x => x.Email == email).FirstOrDefault();
                            var check = (from a in db.TwoFactorAuthenticatedUsers where a.AspNetUserID == aspNetUser.Id select a.AspNetUserID).FirstOrDefault();
                            TwoFactorAuthenticatedUser auth = new TwoFactorAuthenticatedUser();
                            if (check == null)
                            {
                                auth = new TwoFactorAuthenticatedUser()
                                {
                                    AspNetUserID = aspNetUser.Id,
                                    Code = code,
                                    CreatedDate = DateTime.Now,
                                    UpdatedDate = DateTime.Now
                                };
                                db.TwoFactorAuthenticatedUsers.Add(auth);
                                db.SaveChanges();
                            }
                            else
                            {
                                auth = new TwoFactorAuthenticatedUser();
                                auth = db.TwoFactorAuthenticatedUsers.Where(a => a.AspNetUserID == aspNetUser.Id).FirstOrDefault();
                                auth.Code = code;
                                auth.UpdatedDate = DateTime.Now;
                                db.SaveChanges();
                            }
                            code = "Your two factor authentication code is below \n" + code;
                            EmailManager.SendEmail(code, email, "Two Factor Authentication Code");

                            //db.SaveChanges();
                            return new JsonResponse()
                            {
                                Flag = true, Message = "Successfully Sent", ReferenceKey = auth.AspNetUserID
                            };
                        }
                    }
                    catch (Exception ex)
                    {
                        return new JsonResponse()
                        {
                            Flag = false, Message = ex.Message
                        };
                    }
                }
            }));
        }