Update migration to enable all scenarios out of the box

This commit is contained in:
Javier Calvarro Nelson 2017-08-23 17:28:59 -07:00
parent b865d58786
commit 8112557164
6 changed files with 115 additions and 57 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.26815.2 VisualStudioVersion = 15.0.26820.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0F647068-6602-4E24-B1DC-8ED91481A50A}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0F647068-6602-4E24-B1DC-8ED91481A50A}"
EndProject EndProject

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity.Service; using Microsoft.AspNetCore.Identity.Service;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
@ -305,44 +306,95 @@ namespace IdentityOIDCWebApplicationSample.Identity.Data.Migrations
column: "NormalizedUserName", column: "NormalizedUserName",
unique: true); unique: true);
// Seed client application // Seed client applications
var clientAppId = "4122031F-D3A2-4C1A-B25E-2A55B2A32FAC"; var integratedClientClientId = "56A33E6A-ADFE-47EA-BBFE-40F4AE4C55BA";
var clientId = "56A33E6A-ADFE-47EA-BBFE-40F4AE4C55BA"; RegisterApplication(migrationBuilder,
migrationBuilder.Sql($@"INSERT INTO AspNetApplications (Id,ClientId,Name) integratedClientClientId,
VALUES (N'{clientAppId}',N'{clientId}',N'IdentityOIDCWebApplicationSample')"); "IdentityOIDCWebApplicationSample",
//migrationBuilder.InsertData( new[] { ApplicationScope.OpenId.Scope },
// table: "AspNetApplications", new[] { "urn:self:aspnet:identity:integrated" },
// columns: new[] { "Id", "ClientId", "Name" }, new[] { "urn:self:aspnet:identity:integrated" });
// values: new object[,]
// {
// { clientAppId, clientId, "IdentityOIDCWebApplicationSample" }
// });
var clientOpenIdScopeId = "7F4F91FE-87F5-41DC-B111-3DC5FC186E35"; var mobileAppId = "06D7C2FB-A66A-41AD-9509-77BDDFAB111B";
migrationBuilder.Sql($@"INSERT INTO AspNetScopes (Id,ApplicationId,Value) RegisterApplication(migrationBuilder,
VALUES (N'{clientOpenIdScopeId}',N'{clientAppId}',N'{ApplicationScope.OpenId.Scope}')"); mobileAppId,
//migrationBuilder.InsertData( "MobileApplication",
// table: "AspNetScopes", new[] { ApplicationScope.OpenId.Scope, ApplicationScope.OfflineAccess.Scope },
// columns: new[] { "Id", "ApplicationId", "Value" }, new[] { "urn:ietf:wg:oauth:2.0:oob" },
// values: new object[,] new[] { "urn:ietf:wg:oauth:2.0:oob" });
// {
// { clientOpenIdScopeId, clientAppId, ApplicationScope.OpenId.Scope },
// });
var clientRedirectUriId = "849B8050-0DEC-4A96-B234-8A08695A1526"; var protectedApi = "6EA3533F-DD7B-4C34-AAAD-3C493B72D7A5";
var clientLogoutRedirectUriId = "9F24EA98-4375-4CE2-A37C-95832F19D75D"; RegisterApplication(migrationBuilder,
migrationBuilder.Sql($@"INSERT INTO AspNetRedirectUris (Id, ApplicationId, IsLogout, Value) protectedApi,
VALUES (N'{clientRedirectUriId}',N'{clientAppId}','false',N'urn:self:aspnet:identity:integrated')"); "ProtectedApi",
migrationBuilder.Sql($@"INSERT INTO AspNetRedirectUris (Id, ApplicationId, IsLogout, Value) new[] { "read" },
VALUES (N'{clientLogoutRedirectUriId}',N'{clientAppId}','true',N'urn:self:aspnet:identity:integrated')"); new string[] { },
//migrationBuilder.InsertData( new string[] { });
// table: "AspNetRedirectUris",
// columns: new[] { "Id", "ApplicationId", "IsLogout", "Value" }, var spaClientId = "CDA53D17-6683-4EA7-B6D7-B6DB23E60DED";
// values: new object[,] RegisterApplication(migrationBuilder,
// { spaClientId,
// { clientRedirectUriId, clientAppId, false, "urn:self:aspnet:identity:integrated"}, "SPA",
// { clientLogoutRedirectUriId, clientAppId, true, "urn:self:aspnet:identity:integrated" } new[] { ApplicationScope.OpenId.Scope },
// }); new[] { "https://localhost:44324/Spa.html" },
new[] { "https://localhost:44324/Spa.html" });
}
private void RegisterApplication(
MigrationBuilder migrationBuilder,
string clientId,
string name,
string[] scopes,
string[] redirectUris,
string[] logoutRedirectUris)
{
var clientAppId = Guid.NewGuid().ToString();
migrationBuilder.InsertData(
table: "AspNetApplications",
columns: new[] { "Id", "ClientId", "Name" },
values: new object[,]
{
{ clientAppId, clientId, name }
});
var scopeValues = new object[scopes.Length, 3];
for (int i = 0; i < scopes.Length; i++)
{
scopeValues.SetValue(Guid.NewGuid().ToString(), i, 0);
scopeValues.SetValue(clientAppId, i, 1);
scopeValues.SetValue(scopes[i], i, 2);
}
migrationBuilder.InsertData(
table: "AspNetScopes",
columns: new[] { "Id", "ApplicationId", "Value" },
values: scopeValues);
var redirectUriValues = new object[redirectUris.Length + logoutRedirectUris.Length, 4];
for (var i = 0; i < redirectUris.Length; i++)
{
var clientRedirectUriId = Guid.NewGuid().ToString();
redirectUriValues.SetValue(clientRedirectUriId, i, 0);
redirectUriValues.SetValue(clientAppId, i, 1);
redirectUriValues.SetValue(false, i, 2);
redirectUriValues.SetValue(redirectUris[i], i, 3);
}
for (var i = redirectUris.Length; i < redirectUris.Length + logoutRedirectUris.Length; i++)
{
var clientLogoutUriId = Guid.NewGuid().ToString();
redirectUriValues.SetValue(clientLogoutUriId, i, 0);
redirectUriValues.SetValue(clientAppId, i, 1);
redirectUriValues.SetValue(true, i, 2);
redirectUriValues.SetValue(logoutRedirectUris[i - redirectUris.Length], i, 3);
}
migrationBuilder.InsertData(
table: "AspNetRedirectUris",
columns: new[] { "Id", "ApplicationId", "IsLogout", "Value" },
values: redirectUriValues);
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)

View File

@ -16,6 +16,8 @@ namespace IdentityOIDCWebApplicationSample.Controllers
return View(); return View();
} }
public IActionResult Spa() => Redirect("Spa.html");
public IActionResult About() public IActionResult About()
{ {
ViewData["Message"] = "Your application description page."; ViewData["Message"] = "Your application description page.";

View File

@ -8,7 +8,7 @@
"ClientId": "56A33E6A-ADFE-47EA-BBFE-40F4AE4C55BA" "ClientId": "56A33E6A-ADFE-47EA-BBFE-40F4AE4C55BA"
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-IdentityOIDCWebApplicationSample-71B323EE-A696-4709-9EA5-C93EFCD9E6FB;Trusted_Connection=True;MultipleActiveResultSets=true" "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-IdentityOIDCWebApplicationSample-ABC3481D-5C0B-4474-955F-8298AC53AD25;Trusted_Connection=True;MultipleActiveResultSets=true"
}, },
"Logging": { "Logging": {
"IncludeScopes": false, "IncludeScopes": false,

View File

@ -36,18 +36,22 @@
<script class="pre"> <script class="pre">
// The current application coordinates were pre-registered in a B2C tenant. // The current application coordinates were pre-registered in a B2C tenant.
var applicationConfig = { var applicationConfig = {
clientID: '4c0e3ab6-3bdc-4eca-80ab-89669d974e13', clientID: 'CDA53D17-6683-4EA7-B6D7-B6DB23E60DED',
redirectUri: "https://localhost:44324/Spa.html",
authority: "https://localhost:44324/tfp/IdentityService/signinsignup", authority: "https://localhost:44324/tfp/IdentityService/signinsignup",
b2cScopes: ["https://localhost/DFC7191F-FF74-42B9-A292-08FEA80F5B20/v2.0/spa/read"], b2cScopes: ["https://localhost/DFC7191F-FF74-42B9-A292-08FEA80F5B20/v2.0/ProtectedApi/read"], // <<issuer>>/<<resourceAppName>>/<<resourceAppScope>>.
webApi: 'https://localhost:44324/Home/Contact', webApi: 'https://localhost:44324/Home/Contact',
}; };
</script> </script>
<script> <script>
"use strict"; "use strict";
var clientApplication = new Msal.UserAgentApplication(applicationConfig.clientID, applicationConfig.authority, function (errorDesc, token, error, tokenType) { var clientApplication = new Msal.UserAgentApplication(
applicationConfig.clientID,
applicationConfig.authority,
function (errorDesc, token, error, tokenType) {
// Called after loginRedirect or acquireTokenPopup // Called after loginRedirect or acquireTokenPopup
}); }, { redirectUri: applicationConfig.redirectUri });
function login() { function login() {
clientApplication.loginPopup(applicationConfig.b2cScopes).then(function (idToken) { clientApplication.loginPopup(applicationConfig.b2cScopes).then(function (idToken) {

View File

@ -29,26 +29,26 @@ namespace NativeWPFClient
{ {
InitializeComponent(); InitializeComponent();
// Local client // Local client
//DataContext = new NativeWPFClientViewModel
//{
// BaseAddress = "https://localhost/",
// RedirectUri = "urn:ietf:wg:oauth:2.0:oob",
// Tenant = "IdentityService",
// Policy = "signinsignup",
// ClientId = "777f6733-a5ef-49d5-bc0a-877e89ed768b",
// Scopes = "read"
//};
DataContext = new NativeWPFClientViewModel DataContext = new NativeWPFClientViewModel
{ {
BaseAddress = "https://login.microsoftonline.com/", BaseAddress = "https://localhost:44324/",
RedirectUri = "urn:ietf:wg:oauth:2.0:oob", RedirectUri = "urn:ietf:wg:oauth:2.0:oob",
Tenant = "jacalvarb2c.onmicrosoft.com", Tenant = "IdentityService",
Policy = "B2C_1_signinsignup", Policy = "signinsignup",
ClientId = "42291769-0dc8-4497-9cbc-d3879783d6e7", ClientId = "06D7C2FB-A66A-41AD-9509-77BDDFAB111B",
Scopes = "https://jacalvarb2c.onmicrosoft.com/ProtectedApi/read" Scopes = "https://localhost/DFC7191F-FF74-42B9-A292-08FEA80F5B20/v2.0/ProtectedApi/read"
}; };
// DataContext = new NativeWPFClientViewModel
// {
// BaseAddress = "https://login.microsoftonline.com/",
// RedirectUri = "urn:ietf:wg:oauth:2.0:oob",
// Tenant = "jacalvarb2c.onmicrosoft.com",
// Policy = "B2C_1_signinsignup",
// ClientId = "42291769-0dc8-4497-9cbc-d3879783d6e7",
// Scopes = "https://jacalvarb2c.onmicrosoft.com/ProtectedApi/read"
// };
ViewModel.Result = "Hit authorize to sign in"; ViewModel.Result = "Hit authorize to sign in";
} }