Skip to content

Commit 153cb11

Browse files
committed
Update README, solution, and add coding guidelines
Updated README.md to clarify `/api/auth/qrcode` usage and adjusted a link to the correct line range. Modified TwoFactorAuthenticationSample.sln to include new solution items: `.github\copilot-instructions.md` and `README.md`. Made a minor change in Program.cs to safely access the `Name` property using the null-forgiving operator. Added `copilot-instructions.md` with comprehensive coding guidelines and best practices.
1 parent ac37207 commit 153cb11

4 files changed

Lines changed: 89 additions & 3 deletions

File tree

.github/copilot-instructions.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## General
2+
3+
- Make only high confidence suggestions when reviewing code changes.
4+
- Always use the latest version C#, currently C# 13 features.
5+
- Write code that is clean, maintainable, and easy to understand.
6+
- Only add comments rarely to explain why a non-intuitive solution was used. The code should be self-explanatory otherwise.
7+
- Don't add the UTF-8 BOM to files unless they have non-ASCII characters.
8+
- Never change global.json unless explicitly asked to.
9+
- Never change package.json or package-lock.json files unless explicitly asked to.
10+
- Never change NuGet.config files unless explicitly asked to.
11+
12+
## Code Style
13+
14+
### Formatting
15+
16+
- Apply code-formatting style defined in `.editorconfig`.
17+
- Use primary constructors where applicable.
18+
- Prefer file-scoped namespace declarations and single-line using directives.
19+
- Insert a newline before the opening curly brace of any code block (e.g., after `if`, `for`, `while`, `foreach`, `using`, `try`, etc.).
20+
- Ensure that the final return statement of a method is on its own line.
21+
- Use pattern matching and switch expressions wherever possible.
22+
- Prefer using collection expressions when possible
23+
- Use `is` pattern matching instead of `as` and null checks
24+
- Use `nameof` instead of string literals when referring to member names.
25+
- Prefer `?.` if applicable (e.g. `scope?.Dispose()`).
26+
- Use `ObjectDisposedException.ThrowIf` where applicable.
27+
- Use `ArgumentNullException.ThrowIfNull` to validate input parameters.
28+
- If you add new code files, ensure they are listed in the csproj file (if other files in that folder are listed there) so they build.
29+
30+
### Nullable Reference Types
31+
32+
- Declare variables non-nullable, and check for `null` at entry points.
33+
- Always use `is null` or `is not null` instead of `== null` or `!= null`.
34+
- Trust the C# null annotations and don't add null checks when the type system says a value cannot be null.
35+
36+
## Architecture and Design Patterns
37+
38+
### Asynchronous Programming
39+
40+
- Provide both synchronous and asynchronous versions of methods where appropriate.
41+
- Use the `Async` suffix for asynchronous methods.
42+
- Return `Task` or `ValueTask` from asynchronous methods.
43+
- Use `CancellationToken` parameters to support cancellation.
44+
- Avoid async void methods except for event handlers.
45+
- Call `ConfigureAwait(false)` on awaited calls to avoid deadlocks.
46+
47+
### Error Handling
48+
49+
- Use appropriate exception types.
50+
- Include helpful error messages.
51+
- Avoid catching exceptions without rethrowing them.
52+
53+
### Performance Considerations
54+
55+
- Be mindful of performance implications, especially for database operations.
56+
- Avoid unnecessary allocations.
57+
- Consider using more efficient code that is expected to be on the hot path, even if it is less readable.
58+
59+
### Implementation Guidelines
60+
61+
- Write code that is secure by default. Avoid exposing potentially private or sensitive data.
62+
- Make code NativeAOT compatible when possible. This means avoiding dynamic code generation, reflection, and other features that are not compatible with NativeAOT. If not possible, mark the code with an appropriate annotation or throw an exception.
63+
64+
## Documentation
65+
66+
- Include XML documentation for all public APIs. Mention the purpose, intent, and 'the why' of the code, so developers unfamiliar with the project can better understand it. If comments already exist, update them to meet the before mentioned criteria if needed. Use the full syntax of XML Doc Comments to make them as awesome as possible including references to types. Don't add any documentation that is obvious for even novice developers by reading the code.
67+
- Add proper `<remarks>` tags with links to relevant documentation where helpful.
68+
- For keywords like `null`, `true` or `false` use `<see langword="*" />` tags.
69+
- Include code examples in documentation where appropriate.
70+
- Overriding members should inherit the XML documentation from the base type via `/// <inheritdoc />`.
71+
72+
## Markdown
73+
- Use Markdown for documentation files (e.g., README.md).
74+
- Use triple backticks for code blocks, JSON snippets and bash commands, specifying the language (e.g., ```csharp, ```json and ```bash).
75+
76+
## Testing
77+
78+
- When adding new unit tests, strongly prefer to add them to existing test code files rather than creating new code files.
79+
- We use xUnit SDK v3 for tests.
80+
- Do not emit "Act", "Arrange" or "Assert" comments.
81+
- Use NSubstitute for mocking in tests.
82+
- Copy existing style in nearby files for test method names and capitalization.
83+
- When running tests, if possible use filters and check test run counts, or look at test logs, to ensure they actually ran.
84+
- Do not finish work with any tests commented out or disabled that were not previously commented out or disabled.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ A sample that showcases how to implement Two-Factor authentication in a Web API
1111

1212
- Call `/api/auth/register` to register a new user
1313
- Call `/api/auth/login` to get a user token (this is not the JWT and expires after 5 minutes)
14-
- Call `/api/auth/qrcode` with the user token to get the QR Code to add the account to the Authenticator app (note: the QR Code can be obtain only once, this is by design in this sample)
14+
- Call `/api/auth/qrcode?token={userToken}` with the user token as query parameter to get the QR Code to add the account to the Authenticator app (note: the QR Code can be obtained only once, this is by design in this sample)
1515
- Call `/api/auth/validate` with the user token and the OTP code to get the actual JWT
1616

1717
The built-in support for Two-Factor authentication in ASP.NET Core lacks some features. We may want to handle the other options that are provided by the [RFC 6238](http://tools.ietf.org/html/rfc6238), for example:
1818

1919
- Getting the time step of OTP verification to check that the code has only been validated once
2020
- Defining the window of time steps that are considered [acceptable](http://tools.ietf.org/html/rfc6238#section-5.2) for validation
2121

22-
In this case, it is possible to take a look to [Otp.Net](https://github.com/kspearrin/Otp.NET) and use it to implement the [OTP verification](https://github.com/marcominerva/TwoFactorAuthenticationSample/blob/master/TwoFactorAuthenticationSample/Program.cs#L169-L174).
22+
In this case, it is possible to take a look to [Otp.Net](https://github.com/kspearrin/Otp.NET) and use it to implement the [OTP verification](https://github.com/marcominerva/TwoFactorAuthenticationSample/blob/master/TwoFactorAuthenticationSample/Program.cs#L164-L169).

TwoFactorAuthenticationSample.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8318F92C-DC77-448B-9C8B-7784D580D488}"
1111
ProjectSection(SolutionItems) = preProject
1212
.editorconfig = .editorconfig
13+
.github\copilot-instructions.md = .github\copilot-instructions.md
1314
Directory.Build.props = Directory.Build.props
15+
README.md = README.md
1416
EndProjectSection
1517
EndProject
1618
Global

TwoFactorAuthenticationSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179

180180
app.MapGet("/api/me", (ClaimsPrincipal user) =>
181181
{
182-
return TypedResults.Ok(new User(user.Identity!.Name));
182+
return TypedResults.Ok(new User(user.Identity!.Name!));
183183
})
184184
.RequireAuthorization();
185185

0 commit comments

Comments
 (0)