Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. [HttpPost]
  2.         [AllowAnonymous]
  3.         [ValidateAntiForgeryToken]
  4.         public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) {
  5.             if (!ModelState.IsValid) {
  6.                 return View(model);
  7.             }
  8.             //dit geeft de JSON als string terug
  9.             string json = RetrieveJSON(model.Email, model.Password);
  10.             // als inloggen foutief is dan retourneert hij []
  11.             if (json.Equals("[]") || json == null || json.Equals("\"[]\"")) {
  12.                 ModelState.AddModelError("", "Login mislukt");
  13.                 return View(model);
  14.             } else {
  15.                 // haalt de aanhalingstekens en de {} uit de json string
  16.                 json = json.Replace("\"", "");
  17.                 json = json.Replace("{", "");
  18.                 json = json.Replace("}", "");
  19.                 // zet JSON String om naar tweedimensionele array | {{Faculteit:FBO}, {TYPE: Student}} etc etc
  20.                 var jsonArray = json.Split(',').Select(x => x.Split(':')).ToArray();
  21.                 Debug.WriteLine(jsonArray.ToString());
  22.                 string userEmail = jsonArray[5][1];
  23.                 using (DidactContext dc = new DidactContext()) {
  24.                     // controleer of de gebruiker al in de database zit
  25.                     var usr = dc.Gebruikers.Where(u => u.Email == userEmail).FirstOrDefault();
  26.  
  27.                     if (usr != null) { //gebruiker bestaat al
  28.                         Session["GebruikerId"] = usr.GebruikerId.ToString();
  29.                         Session["Email"] = usr.Email.ToString();
  30.                         //return RedirectToAction("Manage/Index");
  31.                         return RedirectToLocal(returnUrl);
  32.                     } else { //gebruiker bestaat nog niet
  33.                         string encryptedpass = sha256_hash(model.Password);
  34.                         string voornaam = jsonArray[4][1];
  35.                         string familienaam = jsonArray[1][1];
  36.                         string foto = jsonArray[2][1];
  37.                         string faculteit = jsonArray[0][1];
  38.                         if (jsonArray[3][1].Equals("student")) {
  39.                             Student student = new Student(userEmail, encryptedpass, voornaam, familienaam, foto, faculteit);
  40.                             dc.Gebruikers.Add(student);
  41.                             dc.SaveChanges();
  42.                         } else {
  43.                             Lector lector = new Lector(userEmail, encryptedpass, voornaam, familienaam, foto, faculteit);
  44.                             dc.Gebruikers.Add(lector);
  45.                             dc.SaveChanges();
  46.                         }
  47.  
  48.                     }
  49.  
  50.                     ////////// met da werkt het ook allemaal nie..
  51.                     //var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
  52.                     //GenericPrincipal gebruiker = (GenericPrincipal)User;
  53.                     //User = await UserManager.FindAsync(model.Email, model.Password);
  54.  
  55.  
  56.                     // ----------------------------------------------------------------------------------------------------------------------
  57.                     //Gebruiker laten overerven van ApplicationUser en dan onderstaande uit commentaar halen
  58.                     //==> geeft een error maar geen idee waarom #justdotnetthings
  59.  
  60.                     //var result = await SignInManager.SignInAsync(usr, false, model.RememberMe);
  61.                     //switch (result) {
  62.                     //    case SignInStatus.Success:
  63.                     //        return RedirectToLocal(returnUrl);
  64.                     //    case SignInStatus.Failure:
  65.                     //        ModelState.AddModelError("", "Ongeldige loginpoging.");
  66.                     //        return RedirectToLocal(returnUrl);
  67.                     //    default:
  68.                     return View(model);
  69.                     //        
  70.                     //}
  71.  
  72.                     //----------------------------------------------------------------------------------------------------------------------
  73.                 }
  74.             }
  75.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement