public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var            userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
            InServiceIUser user        = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }
            if (user.User.Status == Lib.Auth.UserStatus.BLOCKED)
            {
                context.SetError("invalid_grant", "The user is blocked.");
                return;
            }
            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager /*, OAuthDefaults.AuthenticationType*/);

            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager /*,CookieAuthenticationDefaults.AuthenticationType*/);

            AuthenticationProperties properties = CreateProperties(user);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            ViewBag.Title     = "Login";
            ViewBag.countries = new SelectList(DB.Countries.OrderBy(c => c.Name), nameof(Country.ID), nameof(Country.Name));
            var Types = from Gender d in Enum.GetValues(typeof(Gender)) select new { ID = (int)d, Name = d.ToString() };

            ViewBag.GenderID = new SelectList(Types, "ID", "Name");
            //  var captchaResponse = await this.ValidateCaptchaV2();
            //  if ((captchaResponse == null && !Request.IsLocal) || (captchaResponse == false)) ModelState.AddModelError(nameof(ReCaptcha), "CAPTCHA validation failed! Retry logging in.");

            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await UserManager.FindAsync(model.Email, InServiceIUser.GetPasswordHash(model.Email, model.Password));

            if (user == null)
            {
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
            await SignInManager.SignInAsync(user, model.RememberMe, model.RememberMe);

            return(RedirectToLocal(returnUrl));
        }
Пример #3
0
 public InServiceIUserIdentity(InServiceIUser user)
 {
     User = user;
     AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
     AddClaim(new Claim(ClaimTypes.Name, user.User.Name));
     if (!String.IsNullOrWhiteSpace(user.Email))
     {
         AddClaim(new Claim(ClaimTypes.Email, user.Email));
     }
     AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", nameof(InServiceIUserIdentity)));
 }
        public static AuthenticationProperties CreateProperties(InServiceIUser user)
        {
            IDictionary <string, string> data = new Dictionary <string, string>
            {
                { "loginID", user.UserName },
                { "userName", user.User.Name },
                { "userID", user.Id },
                { "roleID", user.User.RoleID.ToString() },
                { "rightsID", user.User.AccessRightID.HasValue?user.User.AccessRightID?.ToString():"0" },
            };

            return(new AuthenticationProperties(data));
        }
Пример #5
0
 public ActionResult Reset(ResetViewModel model)
 {
     if (ModelState.IsValid)
     {
         var myid = User.Identity.GetUserId <int>();
         var user = DB.Users.Find(myid);
         user.Hash = InServiceIUser.GetPasswordHash(user.LoginID, model.Password);
         if (TryUpdateModel(user))
         {
             DB.SaveChanges();
             Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
             return(RedirectToAction(nameof(ResetDone)));
         }
     }
     ViewBag.Title = "Reset";
     return(View());
 }
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new InServiceIUser {
                    UserName = model.Email, PhoneNumber = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
 public InServiceIUserPrincipal(InServiceIUser user) => UserIdentity = new InServiceIUserIdentity(user);