Skip to content

Commit

Permalink
prep for custdom
Browse files Browse the repository at this point in the history
  • Loading branch information
cljung committed Nov 9, 2020
1 parent 8751869 commit 577193d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source-code/obj/*
source-code/bin/*
source-code/.vs/*
*.user
samltestapp4*.*
9 changes: 8 additions & 1 deletion source-code/Pages/Logout.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public IActionResult OnGet(string Tenant, string Policy, string SessionId,string

public IActionResult OnPost(string Tenant, string Policy, string SessionId, string NameId, string Issuer, string DCInfo)
{
string b2cloginurl = Tenant.Split('.')[0] + ".b2clogin.com";
string b2cloginurl = "";// Tenant.Split('.')[0] + ".b2clogin.com";
if ( !Tenant.EndsWith(".onmicrosoft.com"))
{
b2cloginurl = Tenant;
} else
{
b2cloginurl = Tenant.Split('.')[0] + ".b2clogin.com";
}

if (!string.IsNullOrEmpty(DCInfo))
{
Expand Down
16 changes: 12 additions & 4 deletions source-code/Pages/SP/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<small id="emailHelp" class="form-text text-muted">The Tenant name with or without onmicrosoft.com.</small>
<div class="text-danger">@Html.ValidationMessageFor(m => m.Tenant)</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.HostName)
@Html.TextBoxFor(m => m.HostName, htmlAttributes: new { @class = "form-control", @placeholder = "Enter Host Name" })
<small id="emailHelp" class="form-text text-muted">Either yourtenant.b2clogin.com or a custom domain name.</small>
<div class="text-danger">@Html.ValidationMessageFor(m => m.Tenant)</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.Policy)
@Html.TextBoxFor(m => m.Policy, htmlAttributes: new { @class = "form-control", @placeholder = "Enter B2C Policy" })
Expand All @@ -35,10 +43,10 @@
<div class="text-danger">@Html.ValidationMessageFor(m => m.Issuer)</div>
</div>
@*<div class="form-group">
@Html.LabelFor(m => m.IsAzureAD)
@Html.CheckBoxFor(m => m.IsAzureAD, new { @checked = "checked" }))
<div class="text-danger">@Html.ValidationMessageFor(m => m.IsAzureAD)</div>
</div>*@
@Html.LabelFor(m => m.IsAzureAD)
@Html.CheckBoxFor(m => m.IsAzureAD, new { @checked = "checked" }))
<div class="text-danger">@Html.ValidationMessageFor(m => m.IsAzureAD)</div>
</div>*@
<input id="Button1" type="submit" value="Login" class="btn btn-primary" />
}

Expand Down
49 changes: 40 additions & 9 deletions source-code/Pages/SP/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class IndexModel : PageModel


[DisplayName("Tenant Name"), Required]
public string Tenant { get; set; } = "azureadb2ctests";
public string Tenant { get; set; } = "azureadb2ctests";

[DisplayName("Host Name"), Required]
public string HostName { get; set; } = "azureadb2ctests.b2clogin.com";

[DisplayName("B2C Policy"),Required]
public string Policy { get; set; } = "B2C_1A_SignUpOrSignin_SamlApp_Local";

Expand All @@ -40,21 +44,33 @@ public IndexModel(IConfiguration configuration)
_configuration = configuration;
}

public IActionResult OnGet(string Tenant, string Policy, string Issuer)
public IActionResult OnGet(string Tenant, string HostName, string Policy, string Issuer)
{
this.Tenant = HttpContext.Session.GetString("Tenant");
this.HostName = HttpContext.Session.GetString("HostName");
this.Policy = HttpContext.Session.GetString("Policy");
this.Issuer = HttpContext.Session.GetString("Issuer");
if (!String.IsNullOrEmpty(Tenant)) {
if (!string.IsNullOrEmpty(Tenant)) {
this.Tenant = Tenant;
}
if (!String.IsNullOrEmpty(Policy)) {
if (!string.IsNullOrEmpty(HostName))
{
this.HostName = HostName;
}
// if still null, build up hostname yourtenant.b2clogin.com from tenant name yourtenant.onmicrosoft.com
if (string.IsNullOrEmpty(this.HostName) )
{
string TenantName = this.Tenant.ToLower()?.Replace(".onmicrosoft.com", "");
this.HostName = TenantName + ".b2clogin.com";
}
if (!string.IsNullOrEmpty(Policy)) {
this.Policy = Policy;
}
if (!String.IsNullOrEmpty(Issuer)) {
if (!string.IsNullOrEmpty(Issuer)) {
this.Issuer = Issuer;
}
if ( null != this.Tenant) HttpContext.Session.SetString("Tenant", this.Tenant);
if ( null != this.HostName) HttpContext.Session.SetString("HostName", this.HostName);
if ( null != this.Policy) HttpContext.Session.SetString("Policy", this.Policy);
if ( null != this.Issuer ) HttpContext.Session.SetString("Issuer", this.Issuer);
return Page();
Expand All @@ -63,21 +79,36 @@ public IActionResult OnGet(string Tenant, string Policy, string Issuer)
/// <summary>
/// This Post Action is used to Generate the AuthN Request and redirect to the B2C Login endpoint
/// </summary>
public IActionResult OnPost(string Tenant,string Policy, string Issuer, string DCInfo, bool IsAzureAD)
public IActionResult OnPost(string Tenant, string HostName, string Policy, string Issuer, string DCInfo, bool IsAzureAD)
{
if (string.IsNullOrEmpty(Policy) || IsAzureAD)
{
return SendAzureAdRequest(Tenant);
}

String TenantId = Tenant.ToLower()?.Replace(".onmicrosoft.com", "");
string SamlRequest = string.Empty;
string b2cloginurl = TenantId + ".b2clogin.com";
string b2cloginurl = HostName.ToLower();
if (!String.IsNullOrEmpty(HostName))
{
b2cloginurl = HostName;
}
else if (!String.IsNullOrEmpty(this.Tenant) && this.Tenant.EndsWith(".onmicrosoft.com"))
{
string TenantName = Tenant.ToLower()?.Replace(".onmicrosoft.com", "");
b2cloginurl = TenantName + ".b2clogin.com";
}


Policy = Policy.StartsWith("B2C_1A_") ? Policy : "B2C_1A_" + Policy;
Tenant = (Tenant.ToLower().Contains("onmicrosoft.com") || Tenant.ToLower().Contains(".net")) ? Tenant : Tenant + ".onmicrosoft.com";
//Tenant = (Tenant.ToLower().Contains("onmicrosoft.com") || Tenant.ToLower().Contains(".net")) ? Tenant : Tenant + ".onmicrosoft.com";
DCInfo = string.IsNullOrWhiteSpace(DCInfo) ? string.Empty : "&" + DCInfo;
Issuer = string.IsNullOrWhiteSpace(Issuer) ? SAMLHelper.GetThisURL(this) : Issuer;

if (null != Tenant) HttpContext.Session.SetString("Tenant", Tenant);
if (null != b2cloginurl) HttpContext.Session.SetString("HostName", b2cloginurl);
if (null != Policy) HttpContext.Session.SetString("Policy", Policy);
if (null != Issuer) HttpContext.Session.SetString("Issuer", Issuer);

string RelayState = SAMLHelper.toB64(Tenant) + "." + SAMLHelper.toB64(Policy) + "." + SAMLHelper.toB64(Issuer);

if (!string.IsNullOrEmpty(DCInfo))
Expand Down

0 comments on commit 577193d

Please sign in to comment.