Skip to content

Commit e6e2fdc

Browse files
remove auth from tests (dotnet#78)
1 parent f316474 commit e6e2fdc

File tree

3 files changed

+1
-172
lines changed

3 files changed

+1
-172
lines changed

fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/AuthorizedEndpointsTests.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

fundamentals/minimal-apis/samples/MinApiTestsSample/IntegrationTests/TokenEndpointTests.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

fundamentals/minimal-apis/samples/MinApiTestsSample/WebMinRouteGroup/Program.cs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,14 @@
1-
using System.IdentityModel.Tokens.Jwt;
2-
using System.Security.Claims;
3-
using Microsoft.AspNetCore.Authentication.JwtBearer;
41
using Microsoft.EntityFrameworkCore;
5-
using Microsoft.IdentityModel.Tokens;
62
using WebMinRouteGroup;
73
using WebMinRouteGroup.Data;
84
using WebMinRouteGroup.Services;
95

106
var builder = WebApplication.CreateBuilder(args);
117

12-
builder.Services.AddAuthentication(o =>
13-
{
14-
o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
15-
}).AddJwtBearer(o =>
16-
{
17-
o.TokenValidationParameters = new TokenValidationParameters
18-
{
19-
ValidIssuer = "http://localhost:5000",
20-
ValidAudience = "http://localhost:5000",
21-
IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String("Efgwc3/0vEGk/+eS0HP0Hw==")),
22-
ValidateIssuer = true,
23-
ValidateAudience = true,
24-
ValidateLifetime = false,
25-
ValidateIssuerSigningKey = true
26-
};
27-
});
28-
29-
builder.Services.AddAuthorization(o => o.AddPolicy("AdminsOnly",
30-
b => b.RequireClaim("admin", "true")));
31-
328
builder.Services.AddTransient<ITodoService, TodoService>();
339
builder.Services.AddSingleton<IEmailService, EmailService>();
3410

35-
builder.Services.AddEndpointsApiExplorer();
36-
builder.Services.AddSwaggerGen();
11+
3712
builder.Services.AddDbContext<TodoGroupDbContext>(options =>
3813
{
3914
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
@@ -46,56 +21,6 @@
4621
var db = scope.ServiceProvider.GetService<TodoGroupDbContext>();
4722
db?.Database.MigrateAsync();
4823

49-
app.UseAuthentication();
50-
app.UseAuthorization();
51-
52-
if (app.Environment.IsDevelopment())
53-
{
54-
app.UseSwagger();
55-
app.UseSwaggerUI();
56-
}
57-
58-
app.MapGet("/", () => "Hello World!");
59-
60-
app.MapGet("/token", (HttpContext context) =>
61-
{
62-
var username = context.Request.Headers["username"].ToString();
63-
var password = context.Request.Headers["password"].ToString();
64-
65-
if (username.Equals("admin") && password.Equals("admin"))
66-
{
67-
var issuer = "http://localhost:5000";
68-
var audience = "http://localhost:5000";
69-
var securityKey = new SymmetricSecurityKey(Convert.FromBase64String("Efgwc3/0vEGk/+eS0HP0Hw=="));
70-
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
71-
72-
var tokenDescriptor = new SecurityTokenDescriptor
73-
{
74-
Audience = audience,
75-
Issuer = issuer,
76-
SigningCredentials = credentials,
77-
Expires = DateTime.UtcNow.AddMinutes(2),
78-
Subject = new ClaimsIdentity(new []
79-
{
80-
new Claim("admin", "true")
81-
})
82-
};
83-
84-
var jwtTokenHandler = new JwtSecurityTokenHandler();
85-
var token = jwtTokenHandler.CreateToken(tokenDescriptor);
86-
87-
return Results.Ok(new
88-
{
89-
access_token = jwtTokenHandler.WriteToken(token)
90-
});
91-
}
92-
93-
return Results.Unauthorized();
94-
}).AllowAnonymous();
95-
96-
app.MapGet("/admin", () => "Authorized Endpoint")
97-
.RequireAuthorization("AdminsOnly");
98-
9924
// todoV1 endpoints
10025
app.MapGroup("/todos/v1")
10126
.MapTodosApiV1()

0 commit comments

Comments
 (0)