Skip to content

Commit

Permalink
Merge pull request dotnet#1070 from dotnet/damianedwards/RazorPagesTw…
Browse files Browse the repository at this point in the history
…eaks

Numerous fixes for Razor Pages project template
  • Loading branch information
phenning authored Jul 14, 2017
2 parents f411f13 + 11a7f96 commit 63b3228
Show file tree
Hide file tree
Showing 48 changed files with 315 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
"Pages/Account/ResetPasswordConfirmation.cshtml",
"Pages/Account/ResetPasswordConfirmation.cshtml.cs",
"Pages/Account/Manage/**",
"Extensions/EmailSenderExtensions.cs",
"Extensions/UrlHelperExtensions.cs",
"Services/**",
"Data/**",
"Extensions/**"
"Data/**"
]
},
{
Expand All @@ -75,13 +76,14 @@
{
"condition": "(!OrganizationalAuth)",
"exclude": [
"AzureAd/**"
"Extensions/AzureAdAuthenticationBuilderExtensions.cs",
"Extensions/AzureAdOptions.cs"
]
},
{
"condition": "(!IndividualB2CAuth)",
"exclude": [
"AzureAdB2C/**"
"Extensions/AzureAdB2C*.cs"
]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@
#if (OrganizationalAuth || IndividualB2CAuth)
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
#if (IndividualB2CAuth)
using Microsoft.AspNetCore.Authentication.Extensions;
#endif
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
#endif
#if (IndividualLocalAuth)
using Microsoft.AspNetCore.Identity;
#endif
using Microsoft.AspNetCore.Mvc;
#if (IndividualLocalAuth)
#if (IndividualLocalAuth)
using Microsoft.Extensions.Logging;
using Company.WebApplication1.Data;
#endif
#endif
#if (IndividualB2CAuth)
using Microsoft.Extensions.Options;
#endif

namespace Company.WebApplication1.Controllers
{
[Route("[controller]/[action]")]
public class AccountController : Controller
{
#if (IndividualLocalAuth)
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger _logger;

public AccountController(
SignInManager<ApplicationUser> signInManager,
ILoggerFactory loggerFactory)
public AccountController(SignInManager<ApplicationUser> signInManager, ILogger<AccountController> logger)
{
_signInManager = signInManager;
_logger = loggerFactory.CreateLogger<AccountController>();
_logger = logger;
}

//
// POST: /Account/Logout
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
Expand All @@ -49,93 +43,70 @@ public async Task<IActionResult> Logout()
return RedirectToPage("/Index");
}
#elseif (OrganizationalAuth)
//
// GET: /Account/SignIn
[HttpGet]
public IActionResult SignIn()
{
var redirectUrl = Url.Page("/Index");
return Challenge(
new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
new AuthenticationProperties { RedirectUri = redirectUrl },
OpenIdConnectDefaults.AuthenticationScheme
);
}

//
// GET: /Account/SignOut
[HttpGet]
public IActionResult SignOut()
{
var callbackUrl = Url.Action(nameof(SignedOut), "Account", values: null, protocol: Request.Scheme);
return SignOut(new AuthenticationProperties { RedirectUri = callbackUrl },
CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme);
}

//
// GET: /Account/SignedOut
[HttpGet]
public IActionResult SignedOut()
{
if (HttpContext.User.Identity.IsAuthenticated)
{
// Redirect to home page if the user is authenticated.
return RedirectToAction("Index");
}

return View();
var callbackUrl = Url.Page("/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
return SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme
);
}
#elseif (IndividualB2CAuth)
private readonly AzureAdB2COptions _options;

public AccountController(IOptions<AzureAdB2COptions> b2cOptions)
{
Options = b2cOptions.Value;
_options = b2cOptions.Value;
}

public AzureAdB2COptions Options { get; set; }

//
// GET: /Account/SignIn
[HttpGet]
public IActionResult SignIn()
{
var redirectUrl = Url.Page("/Index");
return Challenge(
new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
new AuthenticationProperties { RedirectUri = redirectUrl },
OpenIdConnectDefaults.AuthenticationScheme
);
}

[HttpGet]
public IActionResult ResetPassword()
{
var properties = new AuthenticationProperties() { RedirectUri = "/" };
properties.Items[AzureAdB2COptions.PolicyAuthenticationProperty] = Options.ResetPasswordPolicyId;
var redirectUrl = Url.Page("/Index");
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
properties.Items[AzureAdB2COptions.PolicyAuthenticationProperty] = _options.ResetPasswordPolicyId;
return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);
}

[HttpGet]
public IActionResult EditProfile()
{
var properties = new AuthenticationProperties() { RedirectUri = "/" };
properties.Items[AzureAdB2COptions.PolicyAuthenticationProperty] = Options.EditProfilePolicyId;
var redirectUrl = Url.Page("/Index");
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
properties.Items[AzureAdB2COptions.PolicyAuthenticationProperty] = _options.EditProfilePolicyId;
return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);
}

//
// GET: /Account/SignOut
[HttpGet]
public IActionResult SignOut()
{
var callbackUrl = Url.Action(nameof(SignedOut), "Account", values: null, protocol: Request.Scheme);
return SignOut(new AuthenticationProperties { RedirectUri = callbackUrl },
CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme);
}

//
// GET: /Account/SignedOut
[HttpGet]
public IActionResult SignedOut()
public IActionResult SignOut()
{
if (HttpContext.User.Identity.IsAuthenticated)
{
// Redirect to home page if the user is authenticated.
return RedirectToPage("Index");
}

return View();
var callbackUrl = Url.Page("/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
return SignOut(
new AuthenticationProperties { RedirectUri = callbackUrl },
CookieAuthenticationDefaults.AuthenticationScheme,
OpenIdConnectDefaults.AuthenticationScheme
);
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Microsoft.IdentityModel.Tokens;
#endif

namespace Microsoft.AspNetCore.Authentication.Extensions
namespace Microsoft.AspNetCore.Authentication
{
public static class AzureAdAuthenticationBuilderExtensions
{
Expand All @@ -26,7 +26,7 @@ public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builde
return builder;
}

private class ConfigureAzureOptions: IConfigureNamedOptions<OpenIdConnectOptions>
private class ConfigureAzureOptions : IConfigureNamedOptions<OpenIdConnectOptions>
{
private readonly AzureAdOptions _azureOptions;

Expand All @@ -46,8 +46,8 @@ public void Configure(string name, OpenIdConnectOptions options)

options.TokenValidationParameters = new TokenValidationParameters
{
// Instead of using the default validation (validating against a single issuer value, as we do in line of business apps),
// we inject our own multitenant validation logic
// Instead of using the default validation (validating against a single issuer value, as we do in
// line of business apps), we inject our own multitenant validation logic
ValidateIssuer = false,

// If the app is meant to be accessed by entire organizations, add your issuer validation logic here.
Expand All @@ -58,19 +58,19 @@ public void Configure(string name, OpenIdConnectOptions options)

options.Events = new OpenIdConnectEvents
{
OnTicketReceived = (context) =>
OnTicketReceived = context =>
{
// If your authentication logic is based on users then add your logic here
return Task.FromResult(0);
return Task.CompletedTask;
},
OnAuthenticationFailed = (context) =>
OnAuthenticationFailed = context =>
{
context.Response.Redirect("/Home/Error");
context.Response.Redirect("/Error");
context.HandleResponse(); // Suppress the exception
return Task.FromResult(0);
return Task.CompletedTask;
},
// If your application needs to do authenticate single users, add your user validation below.
//OnTokenValidated = (context) =>
//OnTokenValidated = context =>
//{
// return myUserValidationLogic(context.Ticket.Principal);
//}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.AspNetCore.Authentication.Extensions
namespace Microsoft.AspNetCore.Authentication
{
public static class AzureAdB2CAuthenticationBuilderExtensions
{
Expand Down Expand Up @@ -37,9 +37,9 @@ public void Configure(string name, OpenIdConnectOptions options)
options.UseTokenLifetime = true;
options.CallbackPath = _azureOptions.CallbackPath;

options.TokenValidationParameters = new TokenValidationParameters() { NameClaimType = "name" };
options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" };

options.Events = new OpenIdConnectEvents()
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = OnRedirectToIdentityProvider,
OnRemoteFailure = OnRemoteFailure
Expand All @@ -63,7 +63,7 @@ public Task OnRedirectToIdentityProvider(RedirectContext context)
.Replace($"/{defaultPolicy.ToLower()}/", $"/{policy.ToLower()}/");
context.Properties.Items.Remove(AzureAdB2COptions.PolicyAuthenticationProperty);
}
return Task.FromResult(0);
return Task.CompletedTask;
}

public Task OnRemoteFailure(RemoteFailureContext context)
Expand All @@ -82,9 +82,9 @@ public Task OnRemoteFailure(RemoteFailureContext context)
}
else
{
context.Response.Redirect("/Home/Error");
context.Response.Redirect("/Error");
}
return Task.FromResult(0);
return Task.CompletedTask;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
namespace Microsoft.AspNetCore.Authentication.Extensions
namespace Microsoft.AspNetCore.Authentication
{
public class AzureAdB2COptions
{
public const string PolicyAuthenticationProperty = "Policy";

public string ClientId { get; set; }

public string Instance { get; set; }

public string Domain { get; set; }

public string EditProfilePolicyId { get; set; }

public string SignUpSignInPolicyId { get; set; }

public string ResetPasswordPolicyId { get; set; }

public string CallbackPath { get; set; }

public string DefaultPolicy => SignUpSignInPolicyId;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
namespace Microsoft.AspNetCore.Authentication.Extensions
namespace Microsoft.AspNetCore.Authentication
{
public class AzureAdOptions
{
public string ClientId { get; set; }

public string ClientSecret { get; set; }

public string Instance { get; set; }

public string Domain { get; set; }

public string TenantId { get; set; }

public string CallbackPath { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
using System.Threading.Tasks;
using Company.WebApplication1.Services;

namespace Company.WebApplication1.Extensions
namespace Company.WebApplication1.Services
{
public static class EmailSenderExtensions
{
public static async Task SendEmailConfirmationAsync(this IEmailSender emailSender, string email, string link)
public static Task SendEmailConfirmationAsync(this IEmailSender emailSender, string email, string link)
{
await emailSender.SendEmailAsync(email, "Confirm your email",
$"Please confirm your account by clicking this link: <a href='{HtmlEncoder.Default.Encode(link)}'>link</a>");
return emailSender.SendEmailAsync(email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(link)}'>clicking here</a>.");
}

public static Task SendResetPasswordAsync(this IEmailSender emailSender, string email, string callbackUrl)
{
return emailSender.SendEmailAsync(email, "Reset Password",
$"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ public static string EmailConfirmationLink(this IUrlHelper urlHelper, string use
return urlHelper.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { userId = userId, code = code },
values: new { userId, code },
protocol: scheme);
}

public static string ResetPasswordCallbackLink(this IUrlHelper urlHelper, string userId, string code, string scheme)
{
return urlHelper.Page(
"/Account/ResetPassword",
pageHandler: null,
values: new { userId, code },
protocol: scheme);
}
}
Expand Down
Loading

0 comments on commit 63b3228

Please sign in to comment.