Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sample.http to support user creation then token generation from the new users #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TodoBasicWithAuth/AuthApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public async Task GenerateTokenAsync(UserManager<TodoUser> userManager, HttpCont
}

var claims = new List<Claim>();

claims.Add(new Claim("can_view", "true"));

if (user.IsAdmin)
{
claims.Add(new Claim("can_delete", "true"));
claims.Add(new Claim("can_view", "true"));
}

var key = new SymmetricSecurityKey(_jwtSettings.Key);
Expand Down
7 changes: 7 additions & 0 deletions TodoBasicWithAuth/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -31,6 +32,12 @@ static async Task Main(string[] args)

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var userManager = scope.ServiceProvider.GetService<UserManager<TodoUser>>();
await userManager.CreateAsync(new TodoUser { UserName = "admin", IsAdmin = true }, "Pass123456!" );
}

app.UseAuthentication();
app.UseAuthorization();

Expand Down
46 changes: 29 additions & 17 deletions TodoBasicWithAuth/sample.http
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
@token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjYW5fZGVsZXRlIjoidHJ1ZSIsImNhbl92aWV3IjoidHJ1ZSIsImV4cCI6MTU4MTU2NTU2MiwiaXNzIjoiZGVmYXVsdGlzc3VlciIsImF1ZCI6ImRlZmF1bHRhdWRpZW5jZSJ9.XJyk0vIcuy1x4Kdk2O6N9I3Ibg3Qs4dOlFiiQOle2pk
@todo_id = 1
###

// Needs https://marketplace.visualstudio.com/items?itemName=humao.rest-client
// Get all todos (no authentication)
GET http://localhost:5000/api/todos


###

// Authenticate as Admin
POST http://localhost:5000/api/auth/token
//Create regular user
POST http://localhost:5000/api/auth
Content-Type: application/json

{
"username" : "admin",
"password" : "123456"
"username" : "user",
"password" : "Hunter2!"
}

###

// Authenticate as regular user
# @name LoginRegularUser
POST http://localhost:5000/api/auth/token
Content-Type: application/json

{
"username" : "user",
"password" : "hunter2"
"password" : "Hunter2!"
}

@token = {{LoginRegularUser.response.body.token}}
###

// New Todo
POST http://localhost:5000/api/todos
Authorization: Bearer {{token}}
Content-Type: application/json

{
"Name" : "Write unit tests.",
"IsComplete" : false
}

###
Expand All @@ -43,21 +53,23 @@ Authorization: Bearer {{token}}
GET http://localhost:5000/api/todos/{{todo_id}}
Authorization: Bearer {{token}}


###

// New Todo
POST http://localhost:5000/api/todos
Authorization: Bearer {{token}}
// Authenticate as an admin user
# @name LoginAdminUser
POST http://localhost:5000/api/auth/token
Content-Type: application/json

{
"Name" : "Write unit tests.",
"IsComplete" : false
"username" : "admin",
"password" : "Pass123456!"
}

###

// Delete Todo
// Delete Todo. must be authenticated as admim

@admintoken = {{LoginAdminUser.response.body.token}}

DELETE http://localhost:5000/api/todos/{{todo_id}}
Authorization: Bearer {{token}}
Authorization: Bearer {{admintoken}}