示例#1
0
        public async Task <AuthorizeDto> Get()
        {
            var userIdentity = HttpContext.Current.User.Identity;

            var name = HttpContext.Current.User.GetUserEmail()?.ToLower();

            var task = Task.Run(async() => await _adminUserManager.GetAdminUserByEmailAsync(name));

            task.Wait();

            var           adminUser = task.Result;
            AuthorizeDto  dto       = new AuthorizeDto();
            List <string> roles     = new List <string>();

            if (adminUser != null && !adminUser.IsDeleted)
            {
                roles.Add("AdminUser");
                roles.Add("MobileUser");
                dto.Roles   = roles;
                dto.IsAdmin = true;
            }
            else
            {
                roles.Add("MobileUser");
                dto.Roles   = roles;
                dto.IsAdmin = false;
            }
            return(dto);
        }
示例#2
0
        public async Task <ActionResult <UserDto> > Authorize(AuthorizeDto dto)
        {
            var sessionResult = await _lastFm.CreateLastFmSession(dto.Token);

            if (sessionResult.IsFailed)
            {
                return(HandleLastFmError(sessionResult));
            }


            var session = sessionResult.Value;
            var user    = await _userService.FindUserFromUserName(session.LastFmUser);

            if (user == null)
            {
                var userInfo = await _lastFm.GetUserInfo(null, session.Key);

                if (userInfo.IsFailed)
                {
                    return(HandleLastFmError(sessionResult));
                }

                user = await _userService.GetOrCreateUserFromInfo(userInfo.Value);
            }

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, session.LastFmUser),
                new Claim("SessionKey", session.Key)
            };

            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme
                );

            var authProperties = new AuthenticationProperties()
            {
                IsPersistent = true,
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties
                );

            return(Ok(new UserDto()
            {
                Id = user.Id,
                UserName = user.UserName,
                RealName = user.RealName,
                LastFmUrl = user.LastFmUrl,
                ProfilePicture = user.ProfilePicture,
                RegisteredAt = user.RegisteredAt,
                IanaTimezone = user.IanaTimezone,
            }));
        }
示例#3
0
        public async Task <object> Authorize([FromBody] AuthorizeDto model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email);
                var token   = GenerateJwtToken(model.Email, appUser);
                return(new { token });
            }

            throw new Exception($"Failed to authorize user, reason: {result}");
        }
示例#4
0
        public IActionResult Authorize([FromBody] AuthorizeDto dto)
        {
            var users = _userRepository.FindBy(u => u.Email == dto.Email && u.Password == dto.Password);

            if (users.Any())
            {
                return(Ok(new
                {
                    token = new TokenBuilder().Build(dto),
                }));
            }

            return(BadRequest("Bad authorization credentials given"));
        }
示例#5
0
        public IActionResult Authorize(AuthorizeDto authorizeDto)
        {
            long userId = GetUserIdFromAuthToken(authorizeDto.token);

            if (userId == default(long))
            {
                return(Ok(false));
            }
            var roles = _applicationRoleManager.GetRolesForUser(userId).Select(x => x.Name.ToUpper()).ToList();

            if (roles.Intersect(authorizeDto.roles).ToList().Any())
            {
                return(Ok(true));
            }
            return(Ok(false));
        }
示例#6
0
        public async Task <ActionResult <AuthorizeDto> > Post()
        {
            var student = await this.studentService.AuthenticateBasicAsync(this.Request?.Headers["Authorization"]);

            if (student != null)
            {
                var token        = this.studentService.GenerateToken(student, this.key.JWT);
                var userDto      = this.mapper.Map <UserDto>(student);
                var authorizeDto = new AuthorizeDto {
                    Token = token, Data = userDto
                };
                return(this.Ok(authorizeDto));
            }

            return(this.BadRequest("Invalid Credentials"));
        }
示例#7
0
        public string Build(AuthorizeDto User)
        {
            try
            {
                string token = new JwtBuilder()
                               .WithAlgorithm(new HMACSHA512Algorithm())
                               .WithSecret(System.Environment.GetEnvironmentVariable("ASPNETCORE_TOKEN_JWT_SECRETKEY"))
                               .AddClaim("email", User.Email)
                               .AddClaim("expiration", Expiration())
                               .AddClaim("role", @"Chyba admin ¯\_(ツ)_/¯")
                               .Build();

                return(token);
            }
            catch
            {
                throw new Exception("Can't build token signature");
            }
        }