Guest User

Untitled

a guest
Jan 31st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Security;
  7. using REST_magic1311.Models;
  8. using BotDetect.Web.UI.Mvc;
  9. using System.Net.Mail;
  10.  
  11. namespace REST_magic1311.Controllers
  12. {
  13.     public class UserController : Controller
  14.     {
  15.         private Db_User_Validator duv;
  16.         private string key;
  17.  
  18.         // GET: User
  19.         public ActionResult Index()
  20.         {
  21.             return View();
  22.         }
  23.  
  24.         [HttpGet]
  25.         public ActionResult LogIn()
  26.         {
  27.             return View();
  28.         }
  29.  
  30.         [HttpPost]
  31.         public ActionResult LogIn(UserModel user)
  32.         {
  33.             //if(ModelState.IsValid)
  34.             //{
  35.                 if(IsValid(user.Email, user.Password))
  36.                 {
  37.                     FormsAuthentication.SetAuthCookie(user.Email, false);
  38.                     return RedirectToAction("Index", "Home");
  39.                 }
  40.                 else
  41.                 {
  42.                     ModelState.AddModelError("", "Login data is incorrect.");
  43.                 }
  44.             //}
  45.             return View(user);
  46.         }
  47.  
  48.         [HttpGet]
  49.         public ActionResult Registration()
  50.         {
  51.             return View();
  52.         }
  53.  
  54.         [HttpPost]
  55.         [CaptchaValidation("CaptchaCode", "SampleCaptcha", "Incorrect CAPTCHA code!")]
  56.         public ActionResult Registration(UserModel user)
  57.         {
  58.             duv = new Db_User_Validator();
  59.             if (ModelState.IsValid)
  60.             {
  61.                 if (!duv.UserAlreadyCreated(user))
  62.                 {
  63.                     //sending email to the user email with a random generated variable that will have to put
  64.                     long i = 1;
  65.                     foreach (byte b in Guid.NewGuid().ToByteArray())
  66.                     {
  67.                         i *= ((int)b + 1);
  68.                     }
  69.                     key = string.Format("{0:x}", i - DateTime.Now.Ticks);
  70.  
  71.                     string mail = user.Email;
  72.  
  73.                     if (SendVerificationMail(mail, key))
  74.                     {
  75.                         //going to the other view
  76.                         //return Validation(user, key);
  77.                         user.ActivationCode = key;
  78.                         return Validation(user);
  79.                         //return RedirectToAction("Validation", new { usr = user});
  80.                     }
  81.                     /*
  82.                     if (duv.CreateNewUser(user))
  83.                     {
  84.                         //return RedirectToAction("Index", "Home");
  85.                         return View("View", user);
  86.                     }*/
  87.                 }
  88.                 else
  89.                 {
  90.                     ModelState.AddModelError("", "User Already exists!");
  91.                     return View(user);
  92.                 }
  93.             }
  94.  
  95.             return View(user);
  96.         }
  97.  
  98.  
  99.         //testing validation of the user creating
  100.         [HttpGet]
  101.         public ActionResult Validation(UserModel user)
  102.         {
  103.             return View(user);
  104.         }
  105.  
  106.         [HttpPost]
  107.         [ActionName("Validation")]
  108.         public ActionResult ValidationPost(UserModel usr)
  109.         {
  110.             if(usr.ActivationCode == key)
  111.             {
  112.  
  113.             }
  114.             return View();
  115.         }
  116.  
  117.         public ActionResult LogOut()
  118.         {
  119.             FormsAuthentication.SignOut();
  120.             return RedirectToAction("Index", "Home");
  121.         }
  122.  
  123.         private bool IsValid(string email, string password)
  124.         {
  125.             SimpleCrypto.PBKDF2 crypto = new SimpleCrypto.PBKDF2();
  126.             duv = new Db_User_Validator();
  127.             bool isValid = false;
  128.  
  129.             UserModel user = duv.GetUser(email);
  130.             if(user != null)
  131.             {
  132.                 if(user.Password == crypto.Compute(password, user.PasswordSalt))
  133.                 {
  134.                     isValid = true;
  135.                 }
  136.             }
  137.  
  138.             return isValid;
  139.         }
  140.  
  141.  
  142.         private bool SendVerificationMail(string email, string key)
  143.         {
  144.             try
  145.             {
  146.                 MailMessage mail = new MailMessage("[email protected]", email);
  147.                 SmtpClient client = new SmtpClient();
  148.                 client.Port = 25;
  149.                 client.DeliveryMethod = SmtpDeliveryMethod.Network;
  150.                 client.UseDefaultCredentials = false;
  151.                 client.Host = "mail.magic1311.com";
  152.                 client.Credentials = new System.Net.NetworkCredential("[email protected]", "v@l1d@t1i0n");
  153.                 mail.Subject = "Verification code";
  154.                 mail.Body = "This is you key to activate your account \n \n" + key;
  155.                 client.Send(mail);
  156.                 return true;
  157.             }
  158.             catch (Exception ex)
  159.             {
  160.                 return false;
  161.             }
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment