[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M79NN8SSMFCW3AVRPPY7E50Z":3,"skill-same:01M79NN8SSMFCW3AVRPPY7E50Z":43},{"skillNo":4,"name":5,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":18,"linkOnly":19,"markdownContent":20,"markdownBody":20,"frontMatter":21,"refName":22,"commitSha":23,"skillDirectory":24,"forksCount":13,"repositoryHtmlUrl":25,"repositoryHomepageUrl":6,"ownerHtmlUrl":26,"ownerBlogUrl":6,"ownerDescription":27,"updatedAt":28,"install":29,"resources":42},"01M79NN8SSMFCW3AVRPPY7E50Z","dotnet-security-cheat-sheet",null,"JunMystery\u002FAgent-Guidance","JunMystery","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F161468934?v=4","01M79MTMV8F0BY8NK1A92RA8A0","Jun","01M79MTMWCFBKPHN5B25E6V20B",0,"MIT","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fdotnet-security-cheat-sheet\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"# DotNet Security Cheat Sheet\n\n## Introduction\n\nThis cheat sheet focuses on security decisions for modern .NET and ASP.NET Core. Guidance for existing .NET Framework applications is separated at the end.\n\nKeep the runtime and packages supported and patched. Follow the [.NET security announcements](https:\u002F\u002Fgithub.com\u002Fdotnet\u002Fannouncements\u002Fissues?q=is%3Aopen+is%3Aissue+label%3ASecurity), which include ASP.NET Core advisories.\n\n## Authentication and authorization\n\nUse [ASP.NET Core Identity](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity?view=aspnetcore-10.0) or a supported identity provider integration. Do not implement password recovery or session management from scratch.\n\n### Account protection\n\n- Use a store that supports lockout and enable it for the affected users (`LockoutEnabled`). Configure `IdentityOptions.Lockout` and pass `lockoutOnFailure: true` to `PasswordSignInAsync` so failed password attempts count toward lockout. See [Identity lockout configuration](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0#lockout).\n- Apply [login throttling](Authentication_Cheat_Sheet.md#login-throttling) and [multifactor authentication](Multifactor_Authentication_Cheat_Sheet.md). Protect registration and password recovery too; login lockout does not cover those flows.\n- Return consistent [authentication responses](Authentication_Cheat_Sheet.md#authentication-responses), including comparable timing, so login, registration, and recovery do not disclose whether an account exists.\n\n### Passwords\n\n- Use Identity's [`PasswordHasher\u003CTUser>`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fdata-protection\u002Fconsumer-apis\u002Fpassword-hashing?view=aspnetcore-10.0); do not call `KeyDerivation.Pbkdf2` directly to store passwords.\n- Configure [`PasswordHasherOptions.IterationCount`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0#password-hasher-options) for the hash algorithm used by your Identity version to meet the [Password Storage Cheat Sheet's PBKDF2 guidance](Password_Storage_Cheat_Sheet.md#pbkdf2). Benchmark the work factor on the deployment hardware; do not assume the framework default meets current guidance.\n- Set `IdentityOptions.Password.RequiredLength` according to the [password strength guidance](Authentication_Cheat_Sheet.md#implement-proper-password-strength-controls). Disable the `RequireDigit`, `RequireLowercase`, `RequireUppercase`, and `RequireNonAlphanumeric` composition rules. Update scaffolded registration and password-reset validation to match; see [Identity password configuration](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0#password). Also screen for common and breached passwords; length and composition settings do not provide that check.\n\n### Authentication cookies\n\nConfigure Identity's application cookie through [`ConfigureApplicationCookie`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0):\n\n- Set `Cookie.HttpOnly = true` and `Cookie.SecurePolicy = CookieSecurePolicy.Always`. Choose `Cookie.SameSite` to fit the authentication flow; test federated sign-in before tightening it. `HttpOnly` prevents script access to the cookie, but does not stop injected scripts from making authenticated requests.\n- Set an appropriate `ExpireTimeSpan`. With `SlidingExpiration = true`, activity renews the authentication ticket; it is not an absolute session limit. See [cookie authentication options](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fapi\u002Fmicrosoft.aspnetcore.authentication.cookies.cookieauthenticationoptions) and [session expiration](Session_Management_Cheat_Sheet.md#automatic-session-expiration).\n- Ensure security-sensitive changes update the user's security stamp, calling `UserManager.UpdateSecurityStampAsync(user)` when the operation does not already do so. Keep Identity's [security stamp validation](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0#isecuritystampvalidator-and-signout-everywhere) enabled. Existing cookies are rejected at the next stamp validation, subject to `SecurityStampValidatorOptions.ValidationInterval`; revocation is not necessarily immediate.\n\n### Authorization\n\nPrefer a [fallback policy requiring authentication](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthorization\u002Fpolicies?view=aspnetcore-10.0#require-global-user-authentication), and mark intentionally public endpoints with `[AllowAnonymous]` or `AllowAnonymous()`. It covers only requests that reach the authorization middleware; static files served earlier are not protected by it.\n\nAuthentication alone does not grant permission. Apply role or named-policy requirements, such as `[Authorize(Policy = \"...\")]` or `RequireAuthorization(\"...\")`, to privileged operations. Named policies do not inherit the fallback policy; include the authentication requirement.\n\nFor object-specific permissions, use [`IAuthorizationService.AuthorizeAsync`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthorization\u002Fresource-based?view=aspnetcore-10.0) with the resource and requested operation before returning or changing it. Neither the fallback policy nor an `[Authorize]` attribute alone performs this resource check. See [Authorization](Authorization_Cheat_Sheet.md) and [Insecure Direct Object Reference Prevention](Insecure_Direct_Object_Reference_Prevention_Cheat_Sheet.md).\n\n### Redirects\n\nFor local return URLs, use [`LocalRedirect` or check `Url.IsLocalUrl`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fpreventing-open-redirects?view=aspnetcore-10.0) before redirecting. If external destinations are required, select them from a server-controlled allowlist; see [Unvalidated Redirects and Forwards](Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md#preventing-unvalidated-redirects-and-forwards).\n\n## Input handling\n\n### SQL injection\n\n- Prefer Entity Framework LINQ queries. For SQL text, use the [parameterized API supported by your EF Core version](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fef\u002Fcore\u002Fquerying\u002Fsql-queries#passing-parameters), such as `FromSql` or `FromSqlInterpolated`. Never concatenate untrusted values into SQL text passed to `FromSqlRaw` or other command APIs.\n- Parameters represent values, not table or column names. Map any dynamic identifier to a fixed allowlist. Parameterization does not replace authorization or a database account with minimal privileges.\n- See [Query Parameterization](Query_Parameterization_Cheat_Sheet.md) and [Database Security](Database_Security_Cheat_Sheet.md) for examples and database access controls.\n\n### OS command injection\n\nSee the [.NET guidance in the OS Command Injection Defense Cheat Sheet](OS_Command_Injection_Defense_Cheat_Sheet.md#net).\n\n### Cross-site request forgery\n\nWhen the browser sends credentials automatically, including cookies and Windows, Basic, or Digest authentication, protect state-changing operations with [ASP.NET Core antiforgery validation](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fanti-request-forgery?view=aspnetcore-10.0). Do not change state in GET requests.\n\n- In MVC applications, register `AutoValidateAntiforgeryTokenAttribute` globally for unsafe HTTP methods. Razor Pages provide antiforgery protection by default. These MVC filters are not a substitute for the antiforgery configuration required by other endpoint types.\n- Pair token generation with server-side validation. For forms not covered by automatic token generation, use `@Html.AntiForgeryToken()` explicitly. JavaScript requests must send the request token in the configured header or form field, along with the antiforgery cookie.\n- Do not add `IgnoreAntiforgeryToken` merely to make a failing request succeed. Verify how the endpoint authenticates requests and why CSRF protection is unnecessary before excluding it.\n\nSee [Cross-Site Request Forgery Prevention](Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.md) for the threat model and controls beyond framework configuration.\n\n### Cross-site scripting (XSS)\n\nRazor [encodes ordinary variable output in HTML contexts](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fcross-site-scripting?view=aspnetcore-10.0). Do not bypass it with `Html.Raw` or `HtmlString` for untrusted content. HTML encoding does not make a value safe inside JavaScript, CSS, or a URL; follow the context-specific guidance in [Cross-Site Scripting Prevention](Cross_Site_Scripting_Prevention_Cheat_Sheet.md).\n\nInput validation is not a replacement for output encoding. If users must submit HTML, sanitize it with a maintained HTML sanitizer. Use [Content Security Policy](Content_Security_Policy_Cheat_Sheet.md) as an additional defense.\n\n### Serialization and XML\n\n- Do not use [`BinaryFormatter`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fstandard\u002Fserialization\u002Fbinaryformatter-security-guide); it cannot be made secure. Prefer a data-only format such as JSON with `System.Text.Json`, and deserialize into application-defined types. Validate the resulting values before use.\n- Review serializer-specific risks in the [.NET deserialization guidance](Deserialization_Cheat_Sheet.md#net-csharp). A different serializer does not remove the need to validate data or constrain custom converters and type selection.\n- For XML, disable unnecessary external resource resolution and follow the [settings for your .NET parser](XML_External_Entity_Prevention_Cheat_Sheet.md#net). Defaults differ by API and framework version.\n\n## Data protection\n\n### Encryption for storage\n\n- For Windows-local secrets, [`ProtectedData` uses the Windows Data Protection API (DPAPI)](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fstandard\u002Fsecurity\u002Fhow-to-use-data-protection). Choose the scope deliberately: user scope binds decryption to that account, while machine scope permits other accounts on that computer to decrypt. This does not protect against code running with the decrypting account's privileges.\n- Protect ASP.NET Core's Data Protection keys as well as the data they protect. When [overriding key persistence](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fdata-protection\u002Fimplementation\u002Fkey-storage-providers?view=aspnetcore-10.0), configure encryption at rest explicitly and restrict access to the key repository.\n- For other stored data, follow [Cryptographic Storage](Cryptographic_Storage_Cheat_Sheet.md). If using `AesGcm` directly, use the [constructor with an explicit tag size](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Ffundamentals\u002Fsyslib-diagnostics\u002Fsyslib0053) and [never reuse a nonce with the same key](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fapi\u002Fsystem.security.cryptography.aesgcm.encrypt?view=net-10.0).\n\n### Encryption for transmission\n\nUse TLS, not a custom key-exchange protocol. For browser applications, configure [`UseHttpsRedirection` and `UseHsts`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fenforcing-ssl), or enforce them at the reverse proxy. APIs should reject insecure HTTP requests; a redirect cannot undo disclosure in the original request. See [Transport Layer Security](Transport_Layer_Security_Cheat_Sheet.md).\n\n## Configuration and deployment\n\n- Keep secrets out of source code and checked-in configuration. Use [User Secrets](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fapp-secrets) only for development; it is not encrypted storage. Obtain production credentials from a managed secret store. Follow [Secrets Management](Secrets_Management_Cheat_Sheet.md).\n- Run production applications outside the `Development` environment. Use [exception handling middleware](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Ffundamentals\u002Ferror-handling?view=aspnetcore-10.0) and return generic errors; keep stack traces, credentials, and request details out of responses.\n- Apply [HTTP security headers](HTTP_Headers_Cheat_Sheet.md) in the application or reverse proxy, with one place responsible for each policy. Avoid copying a generic header configuration without testing the application's requirements.\n- Use operating-system permissions and isolation for untrusted components. [Code Access Security and partial trust are not supported security boundaries](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fstandard\u002Fsecurity\u002Fsecure-coding-guidelines).\n\n## Legacy .NET Framework\n\nFor secrets, .NET Framework 4.7.1 and later can use [Configuration Builders](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fconfig-builder) to load values from external sources instead of embedding them in `web.config`.\n\n### ASP.NET MVC and Web Forms\n\n- In MVC, generate tokens with `@Html.AntiForgeryToken()` and validate them with `[ValidateAntiForgeryToken]` on state-changing actions. For AJAX, follow [Microsoft's Framework antiforgery guidance](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fweb-api\u002Foverview\u002Fsecurity\u002Fpreventing-cross-site-request-forgery-csrf-attacks#anti-csrf-and-ajax).\n- In Web Forms, set [`ViewStateUserKey`](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Ffundamentals\u002Fcode-analysis\u002Fquality-rules\u002Fca5368) during `Page_Init` or `OnInit` to an unpredictable value unique to the session. This helps protect ViewState postbacks; endpoints that do not validate ViewState still need CSRF protection.\n- Keep production debugging and tracing disabled and configure custom error responses. Use context-appropriate output encoding; request validation alone does not prevent XSS. See [ASP.NET request validation](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fwhitepapers\u002Frequest-validation).\n\n### Forms authentication\n\nIn [Forms Authentication settings](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fapi\u002Fsystem.web.configuration.formsauthenticationconfiguration?view=netframework-4.8.1), set `cookieless=\"UseCookies\"`, `requireSSL=\"true\"`, and an appropriate `timeout`. This keeps authentication tickets out of URLs and restricts their cookies to HTTPS.\n\n### Windows Communication Foundation (WCF)\n\nChoose a binding and security mode that provide the required confidentiality and authentication. [WCF bindings and security](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fframework\u002Fwcf\u002Ffeature-details\u002Fbindings-and-security) documents their differences: `BasicHttpBinding` is unsecured by default but supports HTTPS when configured; `TransportWithMessageCredential` combines transport protection with message credentials. Do not assume a binding name or HTTP method alone secures a service.\n\n## References\n\n- [Microsoft: Secure coding guidelines for .NET](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Fdotnet\u002Fstandard\u002Fsecurity\u002Fsecure-coding-guidelines)\n- [Microsoft: Configure ASP.NET Core Identity](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fauthentication\u002Fidentity-configuration?view=aspnetcore-10.0)\n- [Microsoft: Prevent cross-site request forgery in ASP.NET Core](https:\u002F\u002Flearn.microsoft.com\u002Fen-us\u002Faspnet\u002Fcore\u002Fsecurity\u002Fanti-request-forgery?view=aspnetcore-10.0)\n",{},"main","5d0a87185c0e172c3901e2a6e5916b6d544b8b94","skills\u002Fdotnet-security-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance","https:\u002F\u002Fgithub.com\u002FJunMystery","AI is the typist, I am the architect. Building efficient, secure, and user-centric products through disciplined AI prompt engineering.","2026-11-13 17:41:25",{"source":7,"skillName":5,"commands":30,"prompt":40,"zipAvailable":41},[31,34,37],{"tool":32,"command":33},"npx","npx skills add JunMystery\u002FAgent-Guidance --skill dotnet-security-cheat-sheet",{"tool":35,"command":36},"bunx","bunx skills add JunMystery\u002FAgent-Guidance --skill dotnet-security-cheat-sheet",{"tool":38,"command":39},"pnpm","pnpm dlx skills add JunMystery\u002FAgent-Guidance --skill dotnet-security-cheat-sheet","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fdotnet-security-cheat-sheet\u002FSKILL.md",true,[],[44,50,56,62,68,74],{"skillNo":45,"name":46,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":47,"categories":48,"keywords":49,"publishedAt":18},"01M79QR1E309VJ6CTQKB32AWWA","zero-trust-architecture-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fzero-trust-architecture-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":51,"name":52,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":53,"categories":54,"keywords":55,"publishedAt":18},"01M79QQV7GHYR02DYGN41S23WN","xss-filter-evasion-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxss-filter-evasion-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":57,"name":58,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":59,"categories":60,"keywords":61,"publishedAt":18},"01M79QQNSN7YB8XB61FFSCS3WH","xs-leaks-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxs-leaks-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":63,"name":64,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":65,"categories":66,"keywords":67,"publishedAt":18},"01M79QQHZZJKFDY9132G5A65C2","xpath-injection-prevention-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxpath-injection-prevention-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":69,"name":70,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":71,"categories":72,"keywords":73,"publishedAt":18},"01M79QQB9ZSD3DJG8658EHQXR1","xml-security-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxml-security-cheat-sheet\u002FSKILL.md",[],[],{"skillNo":75,"name":76,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":7,"ownerLogin":8,"ownerAvatarUrl":9,"ownerAccountNo":10,"ownerDisplayName":11,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":77,"categories":78,"keywords":79,"publishedAt":18},"01M79QQ60WKV7PFMXWT9CW8PM6","xml-external-entity-prevention-cheat-sheet","https:\u002F\u002Fgithub.com\u002FJunMystery\u002FAgent-Guidance\u002Fblob\u002Fmain\u002Fskills\u002Fxml-external-entity-prevention-cheat-sheet\u002FSKILL.md",[],[]]