Learn how to add test users to the in-memory database on startup, so you can test authenticated routes.
- [Narrator] The add test data method in the start up class adds some test data to the in memory database every time the application starts up. We can use the same approach to add some test users to the database. In the start up class, at the very bottom, we have add test data, and that's being called from configure. Right above where we're calling add test data, I'm going to add some new code. We'll here say, add some test roles and users. First, I need to pull a role manager and user manager out of the service collection.
So we can say app.applicationservices GetRequiredService of type Role Manager which we need to input from identity and the role manager will be of type UserRoleEntity. And very similar for the user manager pull that from application services as well say user manager of type user entity. Now we can call a new method called AddTestUsers and we'll pass the role manager and the user manager.
Now we can implement this add test users method. I'll use visual studio to generate a stub. We'll make this static. So we'll say rolemanager.CreateAsync new user role called admin. Now this is an Async method so we need to await this, which also means that we need to return a task and mark the method as Async. Now we can create a test user and we'll say our new user is a new user entity and we'll fill out these fields.
By default the email and username fields are identical. Call this guy admin. We'll say that this user was created right now. Now we'll await usermanager.createAsync on the user and also pass a password. We'll say supersecret123! Which is not a particularly secure password and as a last step we'll make sure that user is in the admin role.
Say usermanager.addtoroleAsync. User, put them in the admin role, and then save those changes on the user. Because we're awaiting asynchronous operations in this method and we're returning a task, we need to go back up to the configure method and make sure we wait for this to complete. We'll use this new test user in our routes that require administrator access. Next we'll create a restful route for getting a collection of users.
- REST vs. RPC
- Using HTTP methods (aka verbs)
- Returning JSON
- Creating a new API project
- Building a root controller
- Routing to controllers with templates
- Requiring HTTPS for security
- Creating resources and data models
- Returning data and resources from a controller
- Representing links (HREFs)
- Representing collections
- Sorting and searching collections
- Creating forms
- Caching and compression
- Authentication and authorization for RESTful APIs
Skill Level Intermediate
Building Web APIs with ASP.NET Core (2016)with Chris Woodruff1h 7m Intermediate
Deploying ASP.NET Core Applicationswith Nate Barbettini57m 57s Intermediate
1. REST API Concepts
2. Building a Basic API
3. Securing the API
4. Representing Resources
5. Representing Links
6. Representing Collections
7. Sorting Collections
8. Searching Collections
9. Forms and Modifying Data
10. Caching and Compression
11. Authentication and Authorization
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.