Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Security;
- using REST_magic1311.Models;
- using BotDetect.Web.UI.Mvc;
- using System.Net.Mail;
- namespace REST_magic1311.Controllers
- {
- public class UserController : Controller
- {
- private Db_User_Validator duv;
- private string key;
- // GET: User
- public ActionResult Index()
- {
- return View();
- }
- [HttpGet]
- public ActionResult LogIn()
- {
- return View();
- }
- [HttpPost]
- public ActionResult LogIn(UserModel user)
- {
- //if(ModelState.IsValid)
- //{
- if(IsValid(user.Email, user.Password))
- {
- FormsAuthentication.SetAuthCookie(user.Email, false);
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ModelState.AddModelError("", "Login data is incorrect.");
- }
- //}
- return View(user);
- }
- [HttpGet]
- public ActionResult Registration()
- {
- return View();
- }
- [HttpPost]
- [CaptchaValidation("CaptchaCode", "SampleCaptcha", "Incorrect CAPTCHA code!")]
- public ActionResult Registration(UserModel user)
- {
- duv = new Db_User_Validator();
- if (ModelState.IsValid)
- {
- if (!duv.UserAlreadyCreated(user))
- {
- //sending email to the user email with a random generated variable that will have to put
- long i = 1;
- foreach (byte b in Guid.NewGuid().ToByteArray())
- {
- i *= ((int)b + 1);
- }
- key = string.Format("{0:x}", i - DateTime.Now.Ticks);
- string mail = user.Email;
- if (SendVerificationMail(mail, key))
- {
- //going to the other view
- //return Validation(user, key);
- user.ActivationCode = key;
- return Validation(user);
- //return RedirectToAction("Validation", new { usr = user});
- }
- /*
- if (duv.CreateNewUser(user))
- {
- //return RedirectToAction("Index", "Home");
- return View("View", user);
- }*/
- }
- else
- {
- ModelState.AddModelError("", "User Already exists!");
- return View(user);
- }
- }
- return View(user);
- }
- //testing validation of the user creating
- [HttpGet]
- public ActionResult Validation(UserModel user)
- {
- return View(user);
- }
- [HttpPost]
- [ActionName("Validation")]
- public ActionResult ValidationPost(UserModel usr)
- {
- if(usr.ActivationCode == key)
- {
- }
- return View();
- }
- public ActionResult LogOut()
- {
- FormsAuthentication.SignOut();
- return RedirectToAction("Index", "Home");
- }
- private bool IsValid(string email, string password)
- {
- SimpleCrypto.PBKDF2 crypto = new SimpleCrypto.PBKDF2();
- duv = new Db_User_Validator();
- bool isValid = false;
- UserModel user = duv.GetUser(email);
- if(user != null)
- {
- if(user.Password == crypto.Compute(password, user.PasswordSalt))
- {
- isValid = true;
- }
- }
- return isValid;
- }
- private bool SendVerificationMail(string email, string key)
- {
- try
- {
- SmtpClient client = new SmtpClient();
- client.Port = 25;
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.UseDefaultCredentials = false;
- client.Host = "mail.magic1311.com";
- mail.Subject = "Verification code";
- mail.Body = "This is you key to activate your account \n \n" + key;
- client.Send(mail);
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment