ASP.NET Core 中的策略方案Policy schemes in ASP.NET Core

本文内容

使用身份验证策略方案,可以更方便地使用多种方法。例如,策略方案可能使用 Google 身份验证,并对其他所有内容使用 cookie 身份验证。身份验证策略方案:

  • 可以轻松地将任何身份验证操作转发到另一个方案。
  • 根据请求动态转发。

使用派生 AuthenticationSchemeOptions 和关联的AuthenticationHandler<TOptions >的所有身份验证方案:

  • 是 ASP.NET Core 2.1 及更高版本中自动的策略方案。
  • 可以通过配置方案的选项来启用。
  1. public class AuthenticationSchemeOptions
  2. {
  3. /// <summary>
  4. /// If set, this specifies a default scheme that authentication handlers should
  5. /// forward all authentication operations to, by default. The default forwarding
  6. /// logic checks in this order:
  7. /// 1. The most specific ForwardAuthenticate/Challenge/Forbid/SignIn/SignOut
  8. /// 2. The ForwardDefaultSelector
  9. /// 3. ForwardDefault
  10. /// The first non null result is used as the target scheme to forward to.
  11. /// </summary>
  12. public string ForwardDefault { get; set; }
  13. /// <summary>
  14. /// If set, this specifies the target scheme that this scheme should forward
  15. /// AuthenticateAsync calls to. For example:
  16. /// Context.AuthenticateAsync("ThisScheme") =>
  17. /// Context.AuthenticateAsync("ForwardAuthenticateValue");
  18. /// Set the target to the current scheme to disable forwarding and allow
  19. /// normal processing.
  20. /// </summary>
  21. public string ForwardAuthenticate { get; set; }
  22. /// <summary>
  23. /// If set, this specifies the target scheme that this scheme should forward
  24. /// ChallengeAsync calls to. For example:
  25. /// Context.ChallengeAsync("ThisScheme") =>
  26. /// Context.ChallengeAsync("ForwardChallengeValue");
  27. /// Set the target to the current scheme to disable forwarding and allow normal
  28. /// processing.
  29. /// </summary>
  30. public string ForwardChallenge { get; set; }
  31. /// <summary>
  32. /// If set, this specifies the target scheme that this scheme should forward
  33. /// ForbidAsync calls to.For example:
  34. /// Context.ForbidAsync("ThisScheme")
  35. /// => Context.ForbidAsync("ForwardForbidValue");
  36. /// Set the target to the current scheme to disable forwarding and allow normal
  37. /// processing.
  38. /// </summary>
  39. public string ForwardForbid { get; set; }
  40. /// <summary>
  41. /// If set, this specifies the target scheme that this scheme should forward
  42. /// SignInAsync calls to. For example:
  43. /// Context.SignInAsync("ThisScheme") =>
  44. /// Context.SignInAsync("ForwardSignInValue");
  45. /// Set the target to the current scheme to disable forwarding and allow normal
  46. /// processing.
  47. /// </summary>
  48. public string ForwardSignIn { get; set; }
  49. /// <summary>
  50. /// If set, this specifies the target scheme that this scheme should forward
  51. /// SignOutAsync calls to. For example:
  52. /// Context.SignOutAsync("ThisScheme") =>
  53. /// Context.SignOutAsync("ForwardSignOutValue");
  54. /// Set the target to the current scheme to disable forwarding and allow normal
  55. /// processing.
  56. /// </summary>
  57. public string ForwardSignOut { get; set; }
  58. /// <summary>
  59. /// Used to select a default scheme for the current request that authentication
  60. /// handlers should forward all authentication operations to by default. The
  61. /// default forwarding checks in this order:
  62. /// 1. The most specific ForwardAuthenticate/Challenge/Forbid/SignIn/SignOut
  63. /// 2. The ForwardDefaultSelector
  64. /// 3. ForwardDefault.
  65. /// The first non null result will be used as the target scheme to forward to.
  66. /// </summary>
  67. public Func<HttpContext, string> ForwardDefaultSelector { get; set; }
  68. }

示例Examples

下面的示例演示了结合较低级别方案的更高级别的方案。Google 身份验证用于质询,cookie 身份验证用于所有其他操作:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  4. .AddCookie(options => options.ForwardChallenge = "Google")
  5. .AddGoogle(options => { });
  6. }

下面的示例基于每个请求启用动态选择方案。也就是说,如何混合使用 cookie 和 API 身份验证:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  4. .AddCookie(options =>
  5. {
  6. // For example, can foward any requests that start with /api
  7. // to the api scheme.
  8. options.ForwardDefaultSelector = ctx =>
  9. ctx.Request.Path.StartsWithSegments("/api") ? "Api" : null;
  10. })
  11. .AddYourApiAuth("Api");
  12. }