Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpPost]
- [AllowAnonymous]
- [ValidateAntiForgeryToken]
- public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) {
- if (!ModelState.IsValid) {
- return View(model);
- }
- //dit geeft de JSON als string terug
- string json = RetrieveJSON(model.Email, model.Password);
- // als inloggen foutief is dan retourneert hij []
- if (json.Equals("[]") || json == null || json.Equals("\"[]\"")) {
- ModelState.AddModelError("", "Login mislukt");
- return View(model);
- } else {
- // haalt de aanhalingstekens en de {} uit de json string
- json = json.Replace("\"", "");
- json = json.Replace("{", "");
- json = json.Replace("}", "");
- // zet JSON String om naar tweedimensionele array | {{Faculteit:FBO}, {TYPE: Student}} etc etc
- var jsonArray = json.Split(',').Select(x => x.Split(':')).ToArray();
- Debug.WriteLine(jsonArray.ToString());
- string userEmail = jsonArray[5][1];
- using (DidactContext dc = new DidactContext()) {
- // controleer of de gebruiker al in de database zit
- var usr = dc.Gebruikers.Where(u => u.Email == userEmail).FirstOrDefault();
- if (usr != null) { //gebruiker bestaat al
- Session["GebruikerId"] = usr.GebruikerId.ToString();
- Session["Email"] = usr.Email.ToString();
- //return RedirectToAction("Manage/Index");
- return RedirectToLocal(returnUrl);
- } else { //gebruiker bestaat nog niet
- string encryptedpass = sha256_hash(model.Password);
- string voornaam = jsonArray[4][1];
- string familienaam = jsonArray[1][1];
- string foto = jsonArray[2][1];
- string faculteit = jsonArray[0][1];
- if (jsonArray[3][1].Equals("student")) {
- Student student = new Student(userEmail, encryptedpass, voornaam, familienaam, foto, faculteit);
- dc.Gebruikers.Add(student);
- dc.SaveChanges();
- } else {
- Lector lector = new Lector(userEmail, encryptedpass, voornaam, familienaam, foto, faculteit);
- dc.Gebruikers.Add(lector);
- dc.SaveChanges();
- }
- }
- ////////// met da werkt het ook allemaal nie..
- //var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
- //GenericPrincipal gebruiker = (GenericPrincipal)User;
- //User = await UserManager.FindAsync(model.Email, model.Password);
- // ----------------------------------------------------------------------------------------------------------------------
- //Gebruiker laten overerven van ApplicationUser en dan onderstaande uit commentaar halen
- //==> geeft een error maar geen idee waarom #justdotnetthings
- //var result = await SignInManager.SignInAsync(usr, false, model.RememberMe);
- //switch (result) {
- // case SignInStatus.Success:
- // return RedirectToLocal(returnUrl);
- // case SignInStatus.Failure:
- // ModelState.AddModelError("", "Ongeldige loginpoging.");
- // return RedirectToLocal(returnUrl);
- // default:
- return View(model);
- //
- //}
- //----------------------------------------------------------------------------------------------------------------------
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement