Пример #1
0
        private async Task SaveAccessToken(EPMS.Models.DomainModels.AspNetUser user, ClaimsIdentity identity)
        {
            var userclaims = await UserManager.GetClaimsAsync(user.Id);

            foreach (var at in (
                         from claims in identity.Claims
                         where claims.Type.EndsWith("access_token")
                         select new Claim(claims.Type, claims.Value, claims.ValueType, claims.Issuer)))
            {
                if (!userclaims.Contains(at))
                {
                    await UserManager.AddClaimAsync(user.Id, at);
                }
            }
        }
Пример #2
0
 private void updateSessionValues(AspNetUser user)
 {
     AspNetUser result = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindByEmail(User.Identity.Name);
     string role = HttpContext.GetOwinContext().Get<ApplicationRoleManager>().FindById(result.AspNetRoles.ToList()[0].Id).Name;
     Session["FullName"] = result.FirstName + " " + result.LastName;
     Session["ProfileImage"] = result.ImageName;
     Session["LoginID"] = result.Id;
     Session["RoleName"] = role;
 }
Пример #3
0
        public async Task<ActionResult> RegisterUser(RegisterViewModel model)
        {

            if (!string.IsNullOrEmpty(model.UserId))
            {
                //means update case
                var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>());
                var roleName = roleManager.FindById(model.SelectedRole).Name;
                AspNetUser userResult = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindByEmail(model.Email);
                string userrRoleID = userResult.AspNetRoles.ToList()[0].Id;
                string userRoleName = roleManager.FindById(userrRoleID).Name;
                if (userrRoleID != model.SelectedRole)
                {//meanse change the role
                    UserManager.RemoveFromRole(model.UserId, userRoleName);

                    UserManager.AddToRole(model.UserId, roleName);
                    TempData["message"] = new MessageViewModel { Message = "User has been updated.", IsUpdated = true };

                }

                //UserManager.RemoveFromRoleAsync(User.Id, roleName);

                return RedirectToAction("RegisterLV");
            }

            if (ModelState.IsValid)
            {
                var user = new AspNetUser { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //Setting role
                    //var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>());
               //     var roleManager = HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
               //     var roleName = roleManager.FindById(model.SelectedRole).Name;
              //      UserManager.AddToRole(user.Id, roleName);
                    //UserManager.AddToRoleAsync(user.Id, roleName);

                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code },
                        protocol: Request.Url.Scheme);
                    //await
                    //    UserManager.SendEmailAsync(model.Email, "Confirm your account",
                    //        "Please confirm your account by clicking this link: <a href=\"" + callbackUrl +
                    //        "\">link</a><br>Your Password is:" + model.Password);
                    ViewBag.Link = callbackUrl;
                    TempData["message"] = new MessageViewModel { Message = "Registeration Confirmation email send to the user.", IsSaved = true };
                    return RedirectToAction("Index", "Dashboard/Home");
                }
                AddErrors(result);
                //model.Roles = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>()).Roles.ToList();
            //    model.Roles = HttpContext.GetOwinContext().Get<ApplicationRoleManager>().Roles.ToList();

            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #4
0
        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 AspNetUser { UserName = model.Email, Email = 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);
        }
Пример #5
0
        private async Task SignInAsync(AspNetUser user, bool isPersistent)
        {
            //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
            //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
            //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            //var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            //await SetExternalProperties(identity);

            //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);

            //await SaveAccessToken(user, identity);
        }