· 6 years ago · Aug 12, 2019, 07:18 PM
1namespace testsitegp
2{
3 public class Startup
4 {
5 public Startup(IConfiguration configuration)
6 {
7 Configuration = configuration;
8 }
9 public IConfiguration Configuration { get; }
10
11 // This method gets called by the runtime. Use this method to add services to the container.
12 public void ConfigureServices(IServiceCollection services)
13 {
14 services.AddScoped<ICostomerRepository, CustomerRepository>();
15 services.AddScoped<IOrderItemRepository, OrderItemRepository>();
16 services.AddScoped<IOrderRepository, OrderRepository>();
17 services.AddScoped<IProductRepository, ProductRepository>();
18 services.AddScoped<ISalespersonRepository, SalespersonRepository>();
19
20 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
21
22 var connection = "Server=tcp:testsitegp.database.windows.net,1433;Initial Catalog=H_Plus_Sports;Persist Security Info=False;" +
23 "User ID=-------;Password=---------;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
24
25 services.AddDbContext<H_Plus_SportsContext>(options => options.UseSqlServer(connection));
26
27 services.AddMvc();
28
29 services.AddAuthentication(options =>
30 {
31 options.DefaultAuthenticateScheme = "JwtBearer";
32 options.DefaultChallengeScheme = "JwtBearer";
33 })
34 .AddJwtBearer("JwtBearer", jwtOptions =>
35 {
36 jwtOptions.TokenValidationParameters = new TokenValidationParameters()
37 {
38 IssuerSigningKey = TokenController.SIGNING_KEY,
39 ValidateIssuer = false,
40 ValidateAudience = false,
41 ValidateIssuerSigningKey = true,
42 ValidateLifetime = true,
43 ClockSkew = TimeSpan.FromMinutes(5)
44 };
45 });
46 }
47
48 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
49 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
50 {
51 if (env.IsDevelopment())
52 {
53 app.UseDeveloperExceptionPage();
54 }
55 else
56 {
57 app.UseHsts();
58 }
59
60 app.UseHttpsRedirection();
61 app.UseAuthentication();
62 app.UseMvc();
63 }
64 }
65}
66
67using Microsoft.AspNetCore.Mvc;
68using Microsoft.IdentityModel.Tokens;
69using System;
70using System.IdentityModel.Tokens.Jwt;
71using System.Security.Claims;
72using System.Text;
73
74namespace testsitegp.Controllers
75{
76 public class TokenController : Controller
77 {
78 private const string SECRET_KEY = "GSATDEHFG";
79 public static readonly SymmetricSecurityKey SIGNING_KEY = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET_KEY));
80
81 [HttpGet]
82 [Route("api/Token/{username}/{password}")]
83 public IActionResult Get(string username, string password)
84 {
85 if (username == password)
86 return new ObjectResult(GenerateToken(username));
87 else
88 return BadRequest();
89 }
90
91 private string GenerateToken(string username)
92 {
93 var token = new JwtSecurityToken(
94 claims: new Claim[]
95 {
96 new Claim(ClaimTypes.Name, username)
97 },
98 notBefore: new DateTimeOffset(DateTime.Now).DateTime,
99 expires: new DateTimeOffset(DateTime.Now.AddMinutes(60)).DateTime,
100 signingCredentials: new SigningCredentials(SIGNING_KEY, SecurityAlgorithms.HmacSha256)
101 );
102
103 return new JwtSecurityTokenHandler().WriteToken(token);
104 }
105 }
106}