Implement an OAuth 2.0 Server (Part 07)
Welcome to the seventh part of a series of posts where we will implement an OAuth 2 Server using AspNet.Security.OpenIdConnectServer.
OAuth Client CRUD - Views
This is the second part of adding our OAuth Client management pages. In the previous section we generated a controller, which automatically generated some views for us. In this section, we’ll make those views do what we need them to do.
Details and Delete Views
Delete the following two cshtml files - we don’t need them, because we’ll be rolling their functions into the Edit
view.
Views/OAuthClients/Details.cshtml
Views/OAuthClients/Delete.cshtml
Index
Views/OAuthClients/Index.cshtml
First, since we deleted our GET DELETE
and GET DETAILS
methods, we’re going to want to delete their references from Index. Delete the two <a>
tags near the bottom of index.cshtml
that reference the details
and delete
links.
Second, it doesn’t make sense to display the client secret
so prominently, so let’s change it to be the client id
instead.
@model IEnumerable<OAuthTutorial.Models.OAuth.OAuthClient>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ClientId)
</th>
<th>
@Html.DisplayNameFor(model => model.ClientName)
</th>
<th>
@Html.DisplayNameFor(model => model.ClientDescription)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ClientId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClientName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClientDescription)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.ClientId">Edit</a>
</td>
</tr>
}
</tbody>
</table>
Create
Views/OAuthClients/Create.cshtml
Delete the <div class="form-group">
containing the ClientSecret
inputs.
Also, in the Model
up top, swap out the OAuthClient
for our OAuthClientsViewModes.CreateClientViewModel
.
@model OAuthTutorial.Models.OAuthClientViewModels.CreateClientViewModel
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<h4>OAuthClient</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ClientName" class="control-label"></label>
<input asp-for="ClientName" class="form-control" />
<span asp-validation-for="ClientName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ClientDescription" class="control-label"></label>
<input asp-for="ClientDescription" class="form-control" />
<span asp-validation-for="ClientDescription" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Edit
Views/OAuthClients/Edit.cshtml
Replace the HTML with this:
|
|
Add Redirect URI
and Remove
buttons. They have type=button
, which prevents them from submitting the whole form as a POST
request, and they have references to two JavaScript methods, which we will add shortly.Finally, it’s important to note that the Redirect URI
inputs have name="RediectUris"
. When multiple inputs have the same name
, their values get packaged and sent as a string[]
to the server. This is how we can group them all together as actually being Redirect URIs.
JavaScript
Add the following <Script>
above the @section Scripts
section:
<script>
var count = @Model.RedirectUris.Length;
function AddRdiDiv() {
count += 1;
console.log("ayyy");
$("#rdiDivs").append('<div id="rdiDiv_' + count +'"><input name="RedirectUris" value=""/><button type="button" onclick="RemoveRdiDiv('+count+')">remove</button></div>');
}
function RemoveRdiDiv(id) {
$("#rdiDiv_" + id).remove();
}
</script>
We initialize count
to be however many RedirectURIs
exist in this OAuthClient
, which serves as a way to uniquely identify inputs for addition and removal.
AddRdiDiv()
adds another empty Redirect URI input to the page, and RemoveRdiDiv()
takes one away.
Testing
At this point it’s likely a good time to take a small break and see if our server is behaving.
Start your server, register a new user or login with one you’ve already created.
Note: If you created a user before, deleted the database and regenerated it, you'll remain logged in but encounter errors because you are cookie identified as a now non-existant user. Log out and try again.
Things to try:
- Create a new
OAuth Client Application
- Reset its
Client Secret
- Add some
Redirect URIs
- Delete the application
You can find these options at http://localhost:5000/OAuthClients
Moving On
The demo of this project to this point can be found here on GitHub.
In the next section, we’ll add Scopes to our application.
Posts in this series
- Implement an OAuth 2.0 Server (Part 19)
- Implement an OAuth 2.0 Server (Part 18)
- Implement an OAuth 2.0 Server (Part 17)
- Implement an OAuth 2.0 Server (Part 16)
- Implement an OAuth 2.0 Server (Part 15)
- Implement an OAuth 2.0 Server (Part 14)
- Implement an OAuth 2.0 Server (Part 13)
- Implement an OAuth 2.0 Server (Part 12)
- Implement an OAuth 2.0 Server (Part 11)
- Implement an OAuth 2.0 Server (Part 10)
- Implement an OAuth 2.0 Server (Part 09)
- Implement an OAuth 2.0 Server (Part 08)
- Implement an OAuth 2.0 Server (Part 07)
- Implement an OAuth 2.0 Server (Part 06)
- Implement an OAuth 2.0 Server (Part 05)
- Implement an OAuth 2.0 Server (Part 04)
- Implement an OAuth 2.0 Server (Part 03)
- Implement an OAuth 2.0 Server (Part 02)
- Implement an OAuth 2.0 Server (Part 01)