Beispiel #1
0
        public async Task <ActionResult> Login(LoginModel details /*, string returnUrl*/)
        {
            AppUser user = await UserManager.FindAsync(details.Name, details.Password);

            if (user == null)
            {
                ModelState.AddModelError("", "Некорректное имя или пароль.");
            }
            else
            {
                ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,
                                                                             DefaultAuthenticationTypes.ApplicationCookie);

                ident.AddClaims(LocationClaimsProvide.GetClaims(ident));
                ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident));

                AuthManager.SignOut();
                AuthManager.SignIn(new AuthenticationProperties
                {
                    IsPersistent = true
                }, ident);

                return(Redirect("~/Blog/Index"));
            }

            return(View(details));
        }
Beispiel #2
0
        public async Task <ActionResult> Create(CreateModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AppUser defUser = await UserManager.FindByNameAsync("RandomUser");

                AppUser user = new AppUser
                {
                    UserName   = model.Name,
                    Email      = model.Email,
                    AvatarPath = AppDomain.CurrentDomain.BaseDirectory +
                                 "Content\\UserProfiles\\defaultAvatar.jpg",
                    AvatarImageMimeType = "image/jpeg"
                };

                IdentityResult result =
                    await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // Назначить роль по умолчанию
                    result = await UserManager.AddToRoleAsync(user.Id, "Users");

                    if (result.Succeeded == false)
                    {
                        AddErrorsFromResult(result);
                    }

                    ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,
                                                                                 DefaultAuthenticationTypes.ApplicationCookie);

                    ident.AddClaims(LocationClaimsProvide.GetClaims(ident));
                    ident.AddClaims(ClaimsRoles.CreateRolesFromClaims(ident));

                    AuthManager.SignOut();
                    AuthManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, ident);
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return(Redirect(returnUrl ?? "/"));
        }