Skip to content
This repository was archived by the owner on Dec 24, 2020. It is now read-only.

Commit 06c7047

Browse files
committed
Adhere to the ASP.NET team coding style guidelines
1 parent ea3207b commit 06c7047

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+582
-305
lines changed

samples/Cordova/Backend/Controllers/AuthenticationController.cs

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using Microsoft.AspNetCore.Http.Authentication;
1+
using Backend.Extensions;
2+
using Microsoft.AspNetCore.Http.Authentication;
23
using Microsoft.AspNetCore.Mvc;
3-
using Backend.Extensions;
44

5-
namespace Backend.Controllers {
6-
public class AuthenticationController : Controller {
5+
namespace Backend.Controllers
6+
{
7+
public class AuthenticationController : Controller
8+
{
79
[HttpGet("~/signin")]
8-
public ActionResult SignIn(string returnUrl = null) {
10+
public ActionResult SignIn(string returnUrl = null)
11+
{
912
// Note: the "returnUrl" parameter corresponds to the endpoint the user agent
1013
// will be redirected to after a successful authentication and not
1114
// the redirect_uri of the requesting client application.
@@ -16,21 +19,25 @@ public ActionResult SignIn(string returnUrl = null) {
1619
}
1720

1821
[HttpPost("~/signin")]
19-
public ActionResult SignIn(string provider, string returnUrl) {
22+
public ActionResult SignIn(string provider, string returnUrl)
23+
{
2024
// Note: the "provider" parameter corresponds to the external
2125
// authentication provider choosen by the user agent.
22-
if (string.IsNullOrEmpty(provider)) {
26+
if (string.IsNullOrEmpty(provider))
27+
{
2328
return BadRequest();
2429
}
2530

26-
if (!HttpContext.IsProviderSupported(provider)) {
31+
if (!HttpContext.IsProviderSupported(provider))
32+
{
2733
return BadRequest();
2834
}
2935

3036
// Note: the "returnUrl" parameter corresponds to the endpoint the user agent
3137
// will be redirected to after a successful authentication and not
3238
// the redirect_uri of the requesting client application.
33-
if (string.IsNullOrEmpty(returnUrl)) {
39+
if (string.IsNullOrEmpty(returnUrl))
40+
{
3441
return BadRequest();
3542
}
3643

@@ -41,7 +48,8 @@ public ActionResult SignIn(string provider, string returnUrl) {
4148
}
4249

4350
[HttpGet("~/signout"), HttpPost("~/signout")]
44-
public ActionResult SignOut() {
51+
public ActionResult SignOut()
52+
{
4553
// Instruct the cookies middleware to delete the local cookie created
4654
// when the user agent is redirected from the external identity provider
4755
// after a successful authentication flow (e.g Google or Facebook).

samples/Cordova/Backend/Controllers/AuthorizationController.cs

+56-29
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,36 @@
1515
using Microsoft.AspNetCore.Mvc;
1616
using Microsoft.EntityFrameworkCore;
1717

18-
namespace Backend.Controllers {
19-
public class AuthorizationController : Controller {
18+
namespace Backend.Controllers
19+
{
20+
public class AuthorizationController : Controller
21+
{
2022
private readonly ApplicationContext database;
2123

22-
public AuthorizationController(ApplicationContext database) {
24+
public AuthorizationController(ApplicationContext database)
25+
{
2326
this.database = database;
2427
}
25-
28+
2629
[Authorize, HttpGet("~/connect/authorize")]
27-
public async Task<IActionResult> Authorize(CancellationToken cancellationToken) {
30+
public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
31+
{
2832
// Note: when a fatal error occurs during the request processing, an OpenID Connect response
2933
// is prematurely forged and added to the ASP.NET context by OpenIdConnectServerHandler.
3034
// You can safely remove this part and let ASOS automatically handle the unrecoverable errors
3135
// by switching ApplicationCanDisplayErrors to false in Startup.cs.
3236
var response = HttpContext.GetOpenIdConnectResponse();
33-
if (response != null) {
37+
if (response != null)
38+
{
3439
return View("Error", response);
3540
}
3641

3742
// Extract the authorization request from the ASP.NET environment.
3843
var request = HttpContext.GetOpenIdConnectRequest();
39-
if (request == null) {
40-
return View("Error", new OpenIdConnectResponse {
44+
if (request == null)
45+
{
46+
return View("Error", new OpenIdConnectResponse
47+
{
4148
Error = OpenIdConnectConstants.Errors.ServerError,
4249
ErrorDescription = "An internal error has occurred"
4350
});
@@ -48,8 +55,10 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
4855
// In theory, this null check shouldn't be needed, but a race condition could occur if you
4956
// manually removed the application details from the database after the initial check made by ASOS.
5057
var application = await GetApplicationAsync(request.ClientId, cancellationToken);
51-
if (application == null) {
52-
return View("Error", new OpenIdConnectResponse {
58+
if (application == null)
59+
{
60+
return View("Error", new OpenIdConnectResponse
61+
{
5362
Error = OpenIdConnectConstants.Errors.InvalidClient,
5463
ErrorDescription = "Details concerning the calling client application cannot be found in the database"
5564
});
@@ -61,15 +70,19 @@ public async Task<IActionResult> Authorize(CancellationToken cancellationToken)
6170

6271
[Authorize, FormValueRequired("submit.Accept")]
6372
[HttpPost("~/connect/authorize"), ValidateAntiForgeryToken]
64-
public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
73+
public async Task<IActionResult> Accept(CancellationToken cancellationToken)
74+
{
6575
var response = HttpContext.GetOpenIdConnectResponse();
66-
if (response != null) {
76+
if (response != null)
77+
{
6778
return View("Error", response);
6879
}
6980

7081
var request = HttpContext.GetOpenIdConnectRequest();
71-
if (request == null) {
72-
return View("Error", new OpenIdConnectResponse {
82+
if (request == null)
83+
{
84+
return View("Error", new OpenIdConnectResponse
85+
{
7386
Error = OpenIdConnectConstants.Errors.ServerError,
7487
ErrorDescription = "An internal error has occurred"
7588
});
@@ -81,12 +94,14 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
8194

8295
// Copy the claims retrieved from the external identity provider
8396
// (e.g Google, Facebook, a WS-Fed provider or another OIDC server).
84-
foreach (var claim in HttpContext.User.Claims) {
97+
foreach (var claim in HttpContext.User.Claims)
98+
{
8599
// Allow ClaimTypes.Name to be added in the id_token.
86100
// ClaimTypes.NameIdentifier is automatically added, even if its
87101
// destination is not defined or doesn't include "id_token".
88102
// The other claims won't be visible for the client application.
89-
if (claim.Type == ClaimTypes.Name) {
103+
if (claim.Type == ClaimTypes.Name)
104+
{
90105
claim.SetDestinations(OpenIdConnectConstants.Destinations.AccessToken,
91106
OpenIdConnectConstants.Destinations.IdentityToken);
92107
}
@@ -95,8 +110,10 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
95110
}
96111

97112
var application = await GetApplicationAsync(request.ClientId, cancellationToken);
98-
if (application == null) {
99-
return View("Error", new OpenIdConnectResponse {
113+
if (application == null)
114+
{
115+
return View("Error", new OpenIdConnectResponse
116+
{
100117
Error = OpenIdConnectConstants.Errors.InvalidClient,
101118
ErrorDescription = "Details concerning the calling client application cannot be found in the database"
102119
});
@@ -130,15 +147,19 @@ public async Task<IActionResult> Accept(CancellationToken cancellationToken) {
130147

131148
[Authorize, FormValueRequired("submit.Deny")]
132149
[HttpPost("~/connect/authorize"), ValidateAntiForgeryToken]
133-
public IActionResult Deny(CancellationToken cancellationToken) {
150+
public IActionResult Deny(CancellationToken cancellationToken)
151+
{
134152
var response = HttpContext.GetOpenIdConnectResponse();
135-
if (response != null) {
153+
if (response != null)
154+
{
136155
return View("Error", response);
137156
}
138157

139158
var request = HttpContext.GetOpenIdConnectRequest();
140-
if (request == null) {
141-
return View("Error", new OpenIdConnectResponse {
159+
if (request == null)
160+
{
161+
return View("Error", new OpenIdConnectResponse
162+
{
142163
Error = OpenIdConnectConstants.Errors.ServerError,
143164
ErrorDescription = "An internal error has occurred"
144165
});
@@ -151,9 +172,11 @@ public IActionResult Deny(CancellationToken cancellationToken) {
151172
}
152173

153174
[HttpGet("~/connect/logout")]
154-
public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
175+
public async Task<ActionResult> Logout(CancellationToken cancellationToken)
176+
{
155177
var response = HttpContext.GetOpenIdConnectResponse();
156-
if (response != null) {
178+
if (response != null)
179+
{
157180
return View("Error", response);
158181
}
159182

@@ -163,8 +186,10 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
163186
var identity = await HttpContext.Authentication.AuthenticateAsync(OpenIdConnectServerDefaults.AuthenticationScheme);
164187

165188
var request = HttpContext.GetOpenIdConnectRequest();
166-
if (request == null) {
167-
return View("Error", new OpenIdConnectResponse {
189+
if (request == null)
190+
{
191+
return View("Error", new OpenIdConnectResponse
192+
{
168193
Error = OpenIdConnectConstants.Errors.ServerError,
169194
ErrorDescription = "An internal error has occurred"
170195
});
@@ -175,14 +200,16 @@ public async Task<ActionResult> Logout(CancellationToken cancellationToken) {
175200

176201
[HttpPost("~/connect/logout")]
177202
[ValidateAntiForgeryToken]
178-
public ActionResult Logout() {
203+
public ActionResult Logout()
204+
{
179205
// Returning a SignOutResult will ask the cookies middleware to delete the local cookie created when
180206
// the user agent is redirected from the external identity provider after a successful authentication flow
181207
// and will redirect the user agent to the post_logout_redirect_uri specified by the client application.
182208
return SignOut("ServerCookie", OpenIdConnectServerDefaults.AuthenticationScheme);
183209
}
184-
185-
protected virtual Task<Application> GetApplicationAsync(string identifier, CancellationToken cancellationToken) {
210+
211+
protected virtual Task<Application> GetApplicationAsync(string identifier, CancellationToken cancellationToken)
212+
{
186213
// Retrieve the application details corresponding to the requested client_id.
187214
return (from application in database.Applications
188215
where application.ApplicationID == identifier

samples/Cordova/Backend/Controllers/ResourceController.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
44

5-
namespace Backend.Controllers {
5+
namespace Backend.Controllers
6+
{
67
[Route("api")]
7-
public class ResourceController : Controller {
8+
public class ResourceController : Controller
9+
{
810
[Authorize, HttpGet, Route("message")]
9-
public IActionResult GetMessage() {
11+
public IActionResult GetMessage()
12+
{
1013
var identity = User.Identity as ClaimsIdentity;
11-
if (identity == null) {
14+
if (identity == null)
15+
{
1216
return BadRequest();
1317
}
1418

samples/Cordova/Backend/Extensions/AppBuilderExtensions.cs

+18-9
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,41 @@
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.Http;
55

6-
namespace Backend.Extensions {
7-
public static class AppBuilderExtensions {
6+
namespace Backend.Extensions
7+
{
8+
public static class AppBuilderExtensions
9+
{
810
public static IApplicationBuilder UseWhen(this IApplicationBuilder app,
9-
Func<HttpContext, bool> condition, Action<IApplicationBuilder> configuration) {
10-
if (app == null) {
11+
Func<HttpContext, bool> condition, Action<IApplicationBuilder> configuration)
12+
{
13+
if (app == null)
14+
{
1115
throw new ArgumentNullException(nameof(app));
1216
}
1317

14-
if (condition == null) {
18+
if (condition == null)
19+
{
1520
throw new ArgumentNullException(nameof(condition));
1621
}
1722

18-
if (configuration == null) {
23+
if (configuration == null)
24+
{
1925
throw new ArgumentNullException(nameof(configuration));
2026
}
2127

2228
var builder = app.New();
2329
configuration(builder);
2430

25-
return app.Use(next => {
31+
return app.Use(next =>
32+
{
2633
builder.Run(next);
2734

2835
var branch = builder.Build();
2936

30-
return context => {
31-
if (condition(context)) {
37+
return context =>
38+
{
39+
if (condition(context))
40+
{
3241
return branch(context);
3342
}
3443

samples/Cordova/Backend/Extensions/HttpContextExtensions.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
using Microsoft.AspNetCore.Http;
55
using Microsoft.AspNetCore.Http.Authentication;
66

7-
namespace Backend.Extensions {
8-
public static class HttpContextExtensions {
9-
public static IEnumerable<AuthenticationDescription> GetExternalProviders(this HttpContext context) {
10-
if (context == null) {
7+
namespace Backend.Extensions
8+
{
9+
public static class HttpContextExtensions
10+
{
11+
public static IEnumerable<AuthenticationDescription> GetExternalProviders(this HttpContext context)
12+
{
13+
if (context == null)
14+
{
1115
throw new ArgumentNullException(nameof(context));
1216
}
1317

@@ -16,8 +20,10 @@ public static IEnumerable<AuthenticationDescription> GetExternalProviders(this H
1620
select description;
1721
}
1822

19-
public static bool IsProviderSupported(this HttpContext context, string provider) {
20-
if (context == null) {
23+
public static bool IsProviderSupported(this HttpContext context, string provider)
24+
{
25+
if (context == null)
26+
{
2127
throw new ArgumentNullException(nameof(context));
2228
}
2329

samples/Cordova/Backend/Helpers/FormValueRequiredAttribute.cs

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,34 @@
33
using Microsoft.AspNetCore.Mvc.ActionConstraints;
44
using Microsoft.AspNetCore.Routing;
55

6-
namespace Backend.Helpers {
7-
public sealed class FormValueRequiredAttribute : ActionMethodSelectorAttribute {
6+
namespace Backend.Helpers
7+
{
8+
public sealed class FormValueRequiredAttribute : ActionMethodSelectorAttribute
9+
{
810
private readonly string _name;
911

10-
public FormValueRequiredAttribute(string name) {
12+
public FormValueRequiredAttribute(string name)
13+
{
1114
_name = name;
1215
}
1316

14-
public override bool IsValidForRequest(RouteContext context, ActionDescriptor action) {
17+
public override bool IsValidForRequest(RouteContext context, ActionDescriptor action)
18+
{
1519
if (string.Equals(context.HttpContext.Request.Method, "GET", StringComparison.OrdinalIgnoreCase) ||
1620
string.Equals(context.HttpContext.Request.Method, "HEAD", StringComparison.OrdinalIgnoreCase) ||
1721
string.Equals(context.HttpContext.Request.Method, "DELETE", StringComparison.OrdinalIgnoreCase) ||
18-
string.Equals(context.HttpContext.Request.Method, "TRACE", StringComparison.OrdinalIgnoreCase)) {
22+
string.Equals(context.HttpContext.Request.Method, "TRACE", StringComparison.OrdinalIgnoreCase))
23+
{
1924
return false;
2025
}
2126

22-
if (string.IsNullOrEmpty(context.HttpContext.Request.ContentType)) {
27+
if (string.IsNullOrEmpty(context.HttpContext.Request.ContentType))
28+
{
2329
return false;
2430
}
2531

26-
if (!context.HttpContext.Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)) {
32+
if (!context.HttpContext.Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase))
33+
{
2734
return false;
2835
}
2936

samples/Cordova/Backend/Models/Application.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
namespace Backend.Models {
2-
public class Application {
1+
namespace Backend.Models
2+
{
3+
public class Application
4+
{
35
public string ApplicationID { get; set; }
46
public string DisplayName { get; set; }
57
public string RedirectUri { get; set; }

0 commit comments

Comments
 (0)