Beispiel #1
0
        private static ClaimsPrincipal getOwinUser()
        {
            HttpContext context = HttpContext.Current;

            if (context == null)
            {
                throw new Exception("UserHelper: HttpContext.Current is null");
            }

            Microsoft.Owin.IOwinContext owinContext = HttpContext.Current.GetOwinContext();
            if (owinContext == null)
            {
                throw new Exception("UserHelper: HttpContext.Current.GetOwinContext() is null");
            }

            Microsoft.Owin.Security.IAuthenticationManager authManager = owinContext.Authentication;
            if (authManager == null)
            {
                throw new Exception("UserHelper: HttpContext.Current.GetOwinContext().Authentication is null");
            }

            ClaimsPrincipal user = authManager.User;

            if (user == null)
            {
                throw new Exception("UserHelper: HttpContext.Current.GetOwinContext().Authentication.User is null");
            }

            return(HttpContext.Current.GetOwinContext().Authentication.User);
        }
 public ActionResult LogOff()
 {
     // We need to remove all session variables between this web app and the client
     Session.RemoveAll();
     // ASP.NET stuff that was here to remove user authorization context.
     Microsoft.Owin.IOwinContext ctx = Request.GetOwinContext();
     Microsoft.Owin.Security.IAuthenticationManager authManager = ctx.Authentication;
     authManager.SignOut("ApplicationCookie");
     return(RedirectToAction("index", "Welcome"));
 }
        public ActionResult SuspendAccount()
        {
            ProfileModels profile = profileRepository.Get(User.Identity.GetUserId());

            profile.IsActive = false;
            profileRepository.Edit(profile);
            profileRepository.Save();

            Microsoft.Owin.Security.IAuthenticationManager AuthenticationManager = HttpContext.GetOwinContext().Authentication;
            AuthenticationManager.SignOut();
            return(RedirectToAction("Index", "Home"));
        }
        public async Task <ActionResult> Login(Models.LogInModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            IdentityUser user = await appUserManager.FindByNameAsync(model.Email);

            if (user.Id != null)
            {
                var result = appUserManager.PasswordHasher.VerifyHashedPassword(user.PasswordHash, model.Password);
                if (result.Equals(PasswordVerificationResult.Success))
                {
                    ClaimsIdentity identity = new ClaimsIdentity(new[]
                    {
                        new Claim(ClaimTypes.Name, user.Name),
                        new Claim(ClaimTypes.Email, user.UserName),
                        new Claim(ClaimTypes.Sid, user.Id)
                    },
                                                                 "ApplicationCookie"
                                                                 );
                    Microsoft.Owin.IOwinContext ctx = Request.GetOwinContext();
                    Microsoft.Owin.Security.IAuthenticationManager authManager = ctx.Authentication;
                    authManager.SignIn(identity);
                    return(Redirect(GetRedirectUrl(model.ReturnURL)));
                }
                else
                {
                    ModelState.AddModelError("Invalid_Login", "Invalid login attempted, please check your username and password");
                    return(View(model));
                }
            }
            else
            {
                ModelState.AddModelError("Invalid_Login", "Invalid login attempted, please check your username: it was not found");
                return(View(model));
            }
        }
Beispiel #5
0
 public ApplicationSignInManager(ApplicationUserManager userManager, Microsoft.Owin.Security.IAuthenticationManager authenticationManager)
     : base(userManager, authenticationManager)
 {
 }
 public AppSignInManager(UserManager <AppUser, int> userManager, Microsoft.Owin.Security.IAuthenticationManager authenticationManager) : base(userManager, authenticationManager)
 {
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="requestContext"></param>
 protected override void Initialize(RequestContext requestContext)
 {
     IOwinContext context = requestContext.HttpContext.GetOwinContext();
     authenticationManager = requestContext.HttpContext.GetOwinContext().Authentication;
     base.Initialize(requestContext);
 }
Beispiel #8
0
 public SignInManager(UserManager usermanager, Microsoft.Owin.Security.IAuthenticationManager aumanager)
     : base(usermanager, aumanager)
 {
 }
        public async Task <ActionResult> Register(Models.RegisterModel model)
        {
            //var s = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray();
            if (ModelState.IsValid)
            {
                // check if email is null or empty
                if (string.IsNullOrEmpty(model.Email))
                {
                    ModelState.AddModelError("empty_email", "Please enter an Email");
                }
                // check if email is a valid email
                else
                {
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"^([A-Za-z][\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    System.Text.RegularExpressions.Match match = regex.Match(model.Email);
                    if (!match.Success)
                    {
                        ModelState.AddModelError("invalid_email", "Provided email is invalid");
                    }
                }
                // check if password is empty or whitespace
                if (string.IsNullOrEmpty(model.Password))
                {
                    ModelState.AddModelError("empty_password", "Please enter a password");
                }
                // check if confirmation password is empty
                if (string.IsNullOrEmpty(model.ConfirmPassword))
                {
                    ModelState.AddModelError("empty_confirmpassword", "Please confirm your password");
                }
                // check if the password and password confirmation are the same
                if (!model.Password.Equals(model.ConfirmPassword))
                {
                    ModelState.AddModelError("missmatching_password", "Password was different from Confirmation");
                }
                // check if name is empty
                if (string.IsNullOrEmpty(model.Name))
                {
                    ModelState.AddModelError("empty_name", "Please enter an alias name");
                }
                if (ModelState.Any(x => x.Value.Errors.Count > 0))
                {
                    return(View(model));
                }
                IdentityUser newUser = new IdentityUser {
                    UserName = model.Email, PasswordHash = model.Password, Name = model.Name
                };
                var result = await appUserManager.CreateAsync(newUser, newUser.PasswordHash);

                if (result.Succeeded)
                {
                    ClaimsIdentity identity = new ClaimsIdentity(new[]
                    {
                        new Claim(ClaimTypes.Name, newUser.Name),
                        new Claim(ClaimTypes.Email, newUser.UserName),
                        new Claim(ClaimTypes.Sid, newUser.Id)
                    },
                                                                 "ApplicationCookie"
                                                                 );
                    Microsoft.Owin.IOwinContext ctx = Request.GetOwinContext();
                    Microsoft.Owin.Security.IAuthenticationManager authManager = ctx.Authentication;
                    authManager.SignIn(identity);
                    return(RedirectToAction("Index", "User"));
                }
                else
                {
                    ModelState.AddModelError("Invalid_Login", "An error occured processing your registration.");
                    return(View(model));
                }
            }
            else
            {
                ModelState.AddModelError("Invalid_Login", "An error occured processing your request.");
                return(View(model));
            }
        }