Beispiel #1
0
        public IActionResult Self()
        {
            SelfViewModel model = new SelfViewModel();
            string        id    = getID();

            if (id == null)
            {
                return(Redirect("/Login/Login"));
            }
            User user = DataBaseAccess.getObject <User>(new User(id));

            model.nickName = user.userNickName;
            model.email    = user.userId;
            model.balance  = user.userMoney;
            model.name     = user.userName;
            model.Icon     = user.userIconID;
            model.IconPath = ImageManager.saveUserIcon(model.Icon);
            if (user.userGender == "m")
            {
                model.gender = "male";
            }
            else
            {
                model.gender = "female";
            }
            return(View(model));
        }
Beispiel #2
0
        public async Task <IDataResponse <SelfViewModel> > GetSelfAsync(CancellationToken token = default)
        {
            var principal = httpContext.HttpContext.User;

            if (principal == null || principal.Identity == null || principal.Identity.Name.IsNullOrEmpty())
            {
                return(new DataResponse <SelfViewModel>(
                           "not_logged_in", System.Net.HttpStatusCode.Conflict, null));
            }

            var user = await signInManager.UserManager.FindByEmailAsync(principal.Identity.Name)
                       .ConfigureAwaitFalse();

            var result = new SelfViewModel
            {
                Id    = user.Id,
                Email = user.Email
            };

            return(new DataResponse <SelfViewModel>(result, null));
        }
Beispiel #3
0
        public async Task <IDataResponse <SelfViewModel> > LoginAsync(
            LoginRequest model, CancellationToken token = default)
        {
            model.ArgumentNullCheck(nameof(model));

            var signInResult = await signInManager.PasswordSignInAsync(
                model.Email, model.Password, model.IsPersistent, false)
                               .ConfigureAwaitFalse();

            if (signInResult.Succeeded)
            {
                var identityUser = await signInManager.UserManager.FindByEmailAsync(model.Email)
                                   .ConfigureAwaitFalse();

                if (!await signInManager.UserManager.IsInRoleAsync(identityUser, Roles.Administrators))
                {
                    if (!await roleManager.RoleExistsAsync(Roles.Administrators).ConfigureAwaitFalse())
                    {
                        await roleManager.CreateAsync(new IdentityRole <int>(Roles.Administrators))
                        .ConfigureAwaitFalse();
                    }

                    await signInManager.UserManager.AddToRoleAsync(identityUser, Roles.Administrators)
                    .ConfigureAwaitFalse();
                }

                var result = new SelfViewModel {
                    Id = identityUser.Id, Email = identityUser.Email
                };

                return(new DataResponse <SelfViewModel>(result, null));
            }
            else
            {
                return(new DataResponse <SelfViewModel>(
                           $"{model.Email}", System.Net.HttpStatusCode.Unauthorized, null));
            }
        }