public async Task <IActionResult> PutFinancialTransaction(int id, FinancialTransaction financialTransaction)
        {
            if (id != financialTransaction.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> PutProfessor(int id, Professor professor)
        {
            if (id != professor.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Beispiel #3
0
        public async Task <IActionResult> PutCourse(int id, Course course)
        {
            if (id != course.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Beispiel #4
0
        public async Task <IActionResult> Login([FromBody] LoginRequest loginRequest)
        {
            if (_db.Managers.Count() == 0)
            {
                byte[] salt = new byte[128 / 8];
                using (var rng = RandomNumberGenerator.Create())
                {
                    rng.GetBytes(salt);
                }
                string phashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                            password: "******",
                                                            salt: salt,
                                                            prf: KeyDerivationPrf.HMACSHA1,
                                                            iterationCount: 10000,
                                                            numBytesRequested: 256 / 8));

                _db.Managers.Add(new Manager {
                    Firstname = "مدیر",
                    Lastname  = "ُسیستم",
                    Password  = phashed,
                    Mobile    = "مدیر",
                    Salt      = salt
                });
                await _db.SaveChangesAsync();
            }
            IPerson user;

            user = await _db.Managers.FirstOrDefaultAsync(m => m.Mobile == loginRequest.Mobile);

            if (user == null)
            {
                user = await _db.Professors.FirstOrDefaultAsync(m => m.Mobile == loginRequest.Mobile);
            }
            if (user == null)
            {
                user = await _db.Interns.FirstOrDefaultAsync(m => m.Mobile == loginRequest.Mobile);
            }
            if (user == null)
            {
                return(Ok(new LoginResponse {
                    IsAuthenticated = false,
                    Message = "نام کاربری یا کلمه ی عبور صحیح نمی باشد"
                }));
            }
            string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                       password: loginRequest.Password,
                                                       salt: user.Salt,
                                                       prf: KeyDerivationPrf.HMACSHA1,
                                                       iterationCount: 10000,
                                                       numBytesRequested: 256 / 8));

            if (hashed == user.Password)
            {
                var tokenHandler    = new JwtSecurityTokenHandler();
                var key             = Encoding.ASCII.GetBytes("fgdbdaxzcvDSG@!#%cgbfdfghsdbg");
                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Subject = new ClaimsIdentity(new[] {
                        new Claim("Mobile", user.Mobile.ToString()),
                        new Claim("Role", user.Role)
                    }),
                    Expires            = DateTime.UtcNow.AddDays(7),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };
                var token = tokenHandler.CreateToken(tokenDescriptor);

                return(Ok(new LoginResponse {
                    IsAuthenticated = true,
                    Message = "با موفقیت وارد شدید",
                    Token = tokenHandler.WriteToken(token),
                    Role = user.Role
                }));
            }
            return(Ok(new LoginResponse {
                IsAuthenticated = false,
                Message = "نام کاربری یا کلمه ی عبور صحیح نمی باشد"
            }));
        }