public IEnumerable <Usuario> findAll() { using (var db = new KedsContext()) { return(db.Usuarios.ToList <Usuario>()); } }
public Usuario Authenticate([FromServices] IUserService userService, [FromServices] KedsContext db, string username, string password) { var user = db.Usuarios.SingleOrDefault(x => x.Login == username && x.Senha == password); // return null if user not found if (user == null) { return(null); } // authentication successful so generate jwt token var tokenHandler = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, user.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); user.Token = tokenHandler.WriteToken(token); // remove password before returning user.Senha = null; return(user); }
public Usuario Auth(Usuario toLoggin) { using (var db = new KedsContext()) { return(_userService.Authenticate(_userService, db, toLoggin.Login, toLoggin.Senha)); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddTransient <UsuarioDao>(); services.AddTransient <InstituicaoDao>(); services.AddTransient <ProvaDao>(); services.AddTransient <QuestaoDao>(); services.AddTransient <AlternativaDao>(); services.AddTransient <SimuladoDao>(); services.AddTransient <RespostaDao>(); var db = new KedsContext(); services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); // configure strongly typed settings objects var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure <AppSettings>(appSettingsSection); // configure jwt authentication var appSettings = appSettingsSection.Get <AppSettings>(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); // configure DI for application services services.AddScoped <IUserService, UserService>(); }
public IEnumerable <Instituicao> findAll() { try { using (var db = new KedsContext()) { return(db.Instituicoes.ToArray <Instituicao>()); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public Instituicao find(Guid toFind) { try { using (var db = new KedsContext()) { return(db.Instituicoes.Find(toFind)); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public IEnumerable <Questao> findAll() { try { using (var db = new KedsContext()) { return(db.Questoes.Include(alt => alt.Alternativas).ToArray <Questao>()); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public List <Prova> findByInstituicao(Guid toFind) { try { using (var db = new KedsContext()) { return(db.Provas.Where(pr => pr.InstituicaoId == toFind).ToList <Prova>()); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public Simulado add(Simulado toAdd) { try { using (var db = new KedsContext()) { toAdd.Id = Guid.NewGuid(); toAdd.Data = DateTime.Now; if (db.Usuarios.Find(toAdd.UsuarioId) == null) { throw new Exception("Usuário não cadastrado"); } if (db.Provas.Find(toAdd.ProvaId) == null) { throw new Exception("Prova não cadastrada"); } toAdd.Questoes = db.Questoes.Count <Questao>(q => q.ProvaId == toAdd.ProvaId); toAdd.Ultima = 0; var questoes = db.Questoes.Where <Questao>(q => q.ProvaId == toAdd.ProvaId); foreach (Questao questao in questoes) { Resposta resposta = new Resposta(); resposta.Id = Guid.NewGuid(); resposta.QuestaoId = questao.Id; toAdd.Respostas.Add(resposta); } db.Simulados.Add(toAdd); db.SaveChanges(); return(toAdd); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public Instituicao add(Instituicao toAdd) { try { using (var db = new KedsContext()) { toAdd.Id = Guid.NewGuid(); db.Instituicoes.Add(toAdd); db.SaveChanges(); return(toAdd); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }
public Usuario add(Usuario toSave) { try { using (var db = new KedsContext()) { toSave.Id = Guid.NewGuid(); db.Usuarios.Add(toSave); db.SaveChanges(); return(toSave); } } catch (Exception e) { Console.WriteLine(e); throw new Exception("Não foi possível realizar a operação."); } }