protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var authHeader = request.Headers.Authorization;

        if (authHeader == null)
            return base.SendAsync(request, cancellationToken);

        if (authHeader.Scheme != "Basic")
            return base.SendAsync(request, cancellationToken);

        if (String.IsNullOrEmpty(authHeader.Parameter))
            return base.SendAsync(request, cancellationToken);

        var encodedUserPass = authHeader.Parameter.Trim();
        var userPass = Encoding.ASCII.GetString(Convert.FromBase64String(encodedUserPass));
        var parts = userPass.Split(":".ToCharArray());
        var email = parts[0];
        var password = parts[1];
        var mem = new UserMembershipProvider();
        if (!mem.ValidateUserEncoded(email, password))
            return base.SendAsync(request, cancellationToken);

        var i = new RadarIdentity(email, "Basic");
        //var identity = new GenericIdentity(username, "Basic");

        //string[] roles = RadarRoleProvider.GetRolesForUser(email);
        var p = new RadarPrincipal(i);
        //var principal = new GenericPrincipal(i, roles);
        Thread.CurrentPrincipal = p;

        if (HttpContext.Current != null)
            HttpContext.Current.User = p;

        return base.SendAsync(request, cancellationToken);
    }
Esempio n. 2
0
 public ActionResult Login(LoginModel model)
 {
     if (ModelState.IsValid)
     {
         UserMembershipProvider mp = new UserMembershipProvider();
         if (mp.ValidateUser(model.Email, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
             return RedirectToAction("Index", "Home");
         }
         else
         {
             ModelState.AddModelError("", "De gebruikersnaam of het wachtwoord is niet correct.");
         }
     }
     return View(model);
 }
Esempio n. 3
0
        public ActionResult LoginPost(string redirectUrl, Login model)
        {
            ViewBag.redirectUrl = redirectUrl;
            if (ModelState.IsValid)
            {
                UserMembershipProvider mp = new UserMembershipProvider();
                if (mp.ValidateUser(model.Email, model.Password))
                {
                    System.Web.HttpContext.Current.Session["Email"] = model.Email;

                    var users = Adapter.UserRepository.Find(a => a.Email == model.Email, null);
                    if (users != null && users.Any())
                    {
                        User user = users.First();
                        if (user.ApprovedDate == null)
                        {
                            ModelState.AddModelError("", "Je hebt je profiel nog niet geactiveerd met de activatielink in de e-mail.");
                            return View(model);
                        }
                        if (user.LockedDate != null)
                        {
                            ModelState.AddModelError("", "Een administrator heeft je profiel gelockt. Gelieve contact op te nemen met onze support.");
                            return View(model);
                        }

                        user.CreatedDate = DateTime.UtcNow;
                        Adapter.UserRepository.Update(user);
                        Adapter.Save();

                        HttpCookie cookie = new HttpCookie("RadarEmail", model.Email);
                        this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
                        HttpCookie cookieP = new HttpCookie("RadarPassword", user.Password);
                        this.ControllerContext.HttpContext.Response.Cookies.Add(cookieP);

                        if (!String.IsNullOrEmpty(redirectUrl))
                        {
                            byte[] b = Convert.FromBase64String(redirectUrl);
                            string url = System.Text.Encoding.UTF8.GetString(b);
                            return Redirect(url + "?&message=login");
                        }
                        else
                            return Redirect("http://localhost:4911/Radar/app/#/?message=login");
                    }

                }
                else
                    ModelState.AddModelError("", "Het emailadres of het paswoord is niet geldig.");
            }
            return View(model);
        }
 /// <summary>
 /// Implement to include authentication logic and create IPrincipal
 /// </summary>
 protected override bool TryCreatePrincipal(string email, string password, out RadarPrincipal principal)
 {
     principal = null;
     var mem = new UserMembershipProvider();
     if (!mem.ValidateUserEncoded(email, password))
         return false;
     principal = new RadarPrincipal(new RadarIdentity(email, "Basic"));
     return true;
 }
Esempio n. 5
0
        public ActionResult RegisterPost(Register model, string redirectUrl)
        {
            ViewBag.redirectUrl = redirectUrl;
            if (ModelState.IsValid)
            {
                var userModel = model;
                RadarModels.Location loc = new RadarModels.Location();
                loc.Street = model.Location.Street;
                loc.Number = model.Location.Number;
                loc.Box = model.Location.Box;
                loc.Zipcode = model.Location.Zipcode;
                loc.City = model.Location.City;
                loc.Country = model.Location.Country;
                IGeocoder geocoder = new GoogleGeocoder();
                Address[] addresses = geocoder.Geocode(loc.Street + " " + loc.Number + ", " + loc.Zipcode + " " + loc.City + ", " + loc.Country).ToArray();
                if (addresses.Length != 0 && addresses[0].Coordinates != null)
                {
                    loc.Latitude = Convert.ToDecimal(addresses[0].Coordinates.Latitude);
                    loc.Longitude = Convert.ToDecimal(addresses[0].Coordinates.Longitude);
                    Adapter.LocationRepository.Insert(loc);
                    Adapter.Save();
                }
                else
                {
                    ModelState.AddModelError("", "Het adres kon niet worden gevonden.");
                    return View(model);
                }

                UserMembershipProvider mp = new UserMembershipProvider();

                MembershipCreateStatus status;

                UserMembershipUser mu = mp.CreateUserBetter(model.Username, model.Email, model.Gender?"m":"f", model.Password,model.DateOfBirth, model.Bio, loc.LocationId, out status) as UserMembershipUser;

                if (status == MembershipCreateStatus.DuplicateEmail)
                    ModelState.AddModelError("", "Emailadres heeft al een account.");
                else if(status == MembershipCreateStatus.InvalidPassword)
                    ModelState.AddModelError("", "Paswoord is niet sterk genoeg. Moet minimum 5 karakters zijn.");
                else if (status == MembershipCreateStatus.Success)
                {
                    SendMail(userModel);

                    if (!String.IsNullOrEmpty(redirectUrl))
                    {
                        byte[] b = Convert.FromBase64String(redirectUrl);
                        string url = System.Text.Encoding.UTF8.GetString(b);
                        return Redirect(url + "?&message=registered");
                    }
                    else
                        return Redirect("http://localhost:4911/Radar/app/#/?message=registered");
                }
            }
            else
            {
                ModelState.AddModelError("", "De ingevulde gegevens zijn niet correct.");
            }
            return View(model);
        }