public async Task <IActionResult> PutPublisher(int id, Publisher publisher)
        {
            if (id != publisher.PubId)
            {
                return(BadRequest());
            }

            _context.Entry(publisher).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PublisherExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <ActionResult <UserWithToken> > Login([FromBody] User user)
        {
            user = await _context.Users
                   .Include(job => job.Job)
                   .FirstOrDefaultAsync(usr => usr.EmailAddress == user.EmailAddress && usr.Password == user.Password);

            UserWithToken userWithToken = null;

            if (user != null)
            {
                var refreshToken = GenerateRefreshToken();
                user.RefreshTokens.Add(refreshToken);
                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(user);
                userWithToken.RefreshToken = refreshToken.Token;
            }

            if (user == null)
            {
                return(NotFound());
            }
            userWithToken.Token = GenerateAccessToken(user.UserId);
            // sign your token here here



            return(userWithToken);
        }
Beispiel #3
0
        public async Task <IActionResult> PutStore(string id, Store store)
        {
            if (id != store.StoreId)
            {
                return(BadRequest());
            }

            _context.Entry(store).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StoreExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutRole(short id, Role role)
        {
            if (id != role.RoleId)
            {
                return(BadRequest());
            }

            _context.Entry(role).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RoleExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutBook([FromRoute] int id, [FromBody] Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != book.BookId)
            {
                return(BadRequest());
            }

            _context.Entry(book).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #6
0
        public async Task <IActionResult> PutJob(short id, Job job)
        {
            if (id != job.JobId)
            {
                return(BadRequest());
            }

            _context.Entry(job).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!JobExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <UserWithToken> > Login([FromBody] User user)
        {
            user = await _context.Users
                   .Include(u => u.Job)
                   .Where(u => u.UserId == user.UserId && u.Password == user.Password)
                   .FirstOrDefaultAsync();

            UserWithToken userWithToken = null;

            if (user != null)
            {
                RefreshToken refreshToken = GenerateRefreshToken();
                user.RefreshTokens.Add(refreshToken);
                _context.RefreshTokens.Add(refreshToken);

                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(user)
                {
                    RefreshToken = refreshToken.Token
                };
            }

            if (userWithToken == null)
            {
                return(NotFound());
            }

            userWithToken.AccessToken = GenerateAccessToken(user.UserId);
            return(userWithToken);
        }
Beispiel #8
0
        public async Task <ActionResult <Token> > Login([FromBody] dynamic userInfo)
        {
            //这里使用dynamic动态类型传参,属性名必须和传入的json字符串的属性名完全一致,大小写敏感。否则,读取不到参数。
            string email     = userInfo.EmailAddress;
            string pwd       = userInfo.Password;
            User   validUser = await _context.Users
                               .Where(u => u.EmailAddress == email && u.Password == pwd)
                               .FirstOrDefaultAsync();

            if (validUser == null)
            {
                return(NotFound());
            }
            else
            {
                RefreshToken refreshToken = createRefreshToken();
                validUser.RefreshTokens.Add(refreshToken);
                await _context.SaveChangesAsync();

                Token token = new Token
                {
                    JWT          = createJWT(validUser.UserId),
                    RefreshToken = refreshToken.Token,
                };
                return(token);
            }
        }
Beispiel #9
0
        public async Task <IActionResult> PutBookAuthor(int id, BookAuthor bookAuthor)
        {
            if (id != bookAuthor.AuthorId)
            {
                return(BadRequest());
            }

            _context.Entry(bookAuthor).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookAuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Create([Bind("AuthorId,LastName,FirstName,Phone,Address,City,State,Zip,EmailAddress")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
        public async Task <IActionResult> Create([Bind("BookId,Title,Type,PubId,Price,Advance,Royalty,YtdSales,Notes,PublishedDate")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PubId"] = new SelectList(_context.Publisher, "PubId", "PubId", book.PubId);
            return(View(book));
        }
        public async Task <ActionResult <UserWithToken> > Login([FromBody] User user)
        {
            user = await _context.User.Include(u => u.Role)
                   .Where(u => u.EmailAddress == user.EmailAddress &&
                          u.Password == user.Password).FirstOrDefaultAsync();

            UserWithToken userWithToken = null;

            if (user != null)
            {
                RefreshToken refreshToken = GenerateRefreshToken();
                user.RefreshToken.Add(refreshToken);
                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(user);
                userWithToken.RefreshToken = refreshToken.Token;
            }

            if (userWithToken == null)
            {
                return(NotFound());
            }

            //sign your token here here..
            userWithToken.AccessToken = GenerateAccessToken(user.UserId);
            return(userWithToken);
        }