{"id":271266,"date":"2015-12-28T12:04:02","date_gmt":"2015-12-28T09:04:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=271266"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=271266","title":{"rendered":"ASP.NET 5. \u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443"},"content":{"rendered":"<p>       \u041f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0435 ASP.NET WebApi \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Javascript \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0433\u043e API. \u0420\u0435\u0448\u0435\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 ASP.NET 5, \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0440\u0435\u043b\u0438\u0437.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0435 MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u044f \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b cookie-based \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e, \u043d\u043e \u043a\u0440\u043e\u0441\u0441-\u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0443\u043a\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c token-based \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>  Microsoft \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 JwtBearerAuthentication. \u041d\u043e \u043e\u0445\u043e\u0442\u0430 \u0436\u0435 \u0441\u0430\u043c\u043e\u043c\u0443 \u0432\u043e \u0432\u0441\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 BearerAuthentication.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>  \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u043e\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 POST-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0447\u0435\u0440\u0435\u0437 AJAX \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u0435\u0440\u0432\u0435\u0440 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u043a\u0438\u0439 \u0442\u043e\u043a\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430\u0445 \u043e\u0442\u0432\u0435\u0442\u0430. \u041f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043d\u043e\u0432\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u0430 API, \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0439 \u0442\u043e\u043a\u0435\u043d. \u0427\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u043d\u0435 \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c, \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0432 \u043a\u0443\u043a\u0430\u0445 (\u0434\u0430, \u043e\u043f\u044f\u0442\u044c \u043a\u0443\u043a\u0438, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438).<\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f ASP.NET 5 \u2014 RC1 Update1. \u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0430\u043a\u0435\u0442 Microsoft.AspNet.Authentication.<br \/>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <b>BearerAuthenticationExtensions<\/b> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u0435\u0442\u043e\u0434\u044b UseBearerAuthentication \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 IApplicationBuilder. \u0417\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 app.UseMiddleware();<br \/>  <b>BearerAuthenticationMiddleware<\/b> \u2014 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 AuthenticationMiddleware;<br \/>  <b>BearerAuthenticationOptions<\/b> \u2014 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 AuthenticationOptions;<br \/>  <b>BearerAuthenticationHandler<\/b> \u2014 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u043b\u0430\u0441\u0441 AuthenticationHandler \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<br \/>  \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b:<br \/>  <b>BearerAuthenticationDefaults<\/b> \u2014 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b AuthenticationScheme \u0438 HeaderName;<br \/>  <b>IBearerAuthenticationEvents<\/b> \u2014 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u0437 BearerAuthenticationHandler, \u0434\u043b\u044f \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432\u043d\u0435 middleware. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0432 BearerAuthenticationOptions.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043b\u0430\u0441\u0441 BearerAuthenticationOptions.<\/p>\n<pre><code class=\"cs\">public class BearerAuthenticationOptions : AuthenticationOptions, IOptions&lt;BearerAuthenticationOptions&gt; {     public BearerAuthenticationOptions()     {         AuthenticationScheme = BearerAuthenticationDefaults.AuthenticationScheme;         HeaderName = BearerAuthenticationDefaults.HeaderName;         SystemClock = new SystemClock();         Events = new BearerAuthenticationEvents();     }      public string HeaderName { get; set; }      public ISecureDataFormat&lt;AuthenticationTicket&gt; TicketDataFormat { get; set; }      public IDataProtectionProvider DataProtectionProvider { get; set; }      public ISystemClock SystemClock { get; set; }      public IBearerAuthenticationEvents Events { get; set; }      public BearerAuthenticationOptions Value =&gt; this; } <\/code><\/pre>\n<p>  TicketDataFormat \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430. \u0415\u0441\u043b\u0438 TicketDataFormat \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u0445, \u0442\u043e \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e DataProtectionProvider. SystemClock \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0434\u0430\u0442\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0441\u0440\u043e\u043a \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430.<\/p>\n<p>  \u041a\u043b\u0430\u0441\u0441 BearerAuthenticationMiddleware \u0438\u043c\u0435\u0435\u0442 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 CreateHandler(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 BearerAuthenticationHandler.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <b>BearerAuthenticationHandler<\/b>. \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:<\/p>\n<p>  <b>HandleSignInAsync<\/b> \u2014 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0438\u043a\u0435\u0442 (AuthenticationTicket), \u0437\u0430\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430. \u0422\u0438\u043a\u0435\u0442 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0437 ClaimsPrincipal, AuthenticationProperties \u0438 AuthenticationScheme; <br \/>  <b>HandleSignOutAsync<\/b> \u2014 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0443\u0434\u0435\u043c \u0432 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0435 \u043e\u0442\u0432\u0435\u0442\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0442\u0443, \u0447\u0442\u043e\u0431\u044b \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043d\u044f\u043b\u043e \u043f\u0443\u0441\u0442\u043e\u0439 \u0442\u043e\u043a\u0435\u043d;<br \/>  <b>HandleAuthenticateAsync<\/b> \u2014 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u2014 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u0438\u0437 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 \u0432 \u0442\u0438\u043a\u0435\u0442, \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0435\u0433\u043e \u0441\u0440\u043e\u043a \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f;<br \/>  <b>HandleUnauthorizedAsync<\/b> \u2014 \u043d\u0430 \u043d\u0435\u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0441 \u043a\u043e\u0434\u043e\u043c 401;<br \/>  <b>HandleForbiddenAsync<\/b> \u2014 \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043a \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0437\u0430\u043a\u0440\u044b\u0442 \u0434\u043e\u0441\u0442\u0443\u043f, \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u043c \u0441 \u043a\u043e\u0434\u043e\u043c 403;<br \/>  <b>FinishResponseAsync<\/b> \u2014 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>  \u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"cs\">public class BearerAuthenticationHandler : AuthenticationHandler&lt;BearerAuthenticationOptions&gt; {     private bool _shouldRenew;      private AuthenticationTicket GetTicket()     {         if (!Context.Request.Headers.ContainsKey(Options.HeaderName))             return null;         var bearer = Context.Request.Headers[Options.HeaderName];         if (string.IsNullOrEmpty(bearer))             return null;          var ticket = Options.TicketDataFormat.Unprotect(bearer);         if (ticket == null)             return null;          var currentUtc = Options.SystemClock.UtcNow;         var expiresUtc = ticket.Properties.ExpiresUtc;         if (expiresUtc.HasValue && expiresUtc.Value &lt; currentUtc)             return null;          return ticket;     }      private void ApplyBearer(AuthenticationTicket ticket)     {         if (ticket != null)         {             var protectedData = Options.TicketDataFormat.Protect(ticket);             Response.Headers[&quot;Access-Control-Expose-Headers&quot;] = Options.HeaderName;             Response.Headers[Options.HeaderName] = protectedData;         }         else         {             Response.Headers[&quot;Access-Control-Expose-Headers&quot;] = Options.HeaderName;             Response.Headers[Options.HeaderName] = StringValues.Empty;         }     }      protected override async Task HandleSignInAsync(SignInContext signIn)     {         var signingInContext = new BearerSigningInContext(Context, Options, signIn.Principal, new AuthenticationProperties(signIn.Properties));         await Options.Events.SigningIn(signingInContext);          var ticket = new AuthenticationTicket(signingInContext.Principal, signingInContext.Properties, Options.AuthenticationScheme);         ApplyBearer(ticket);          var signedInContext = new BearerSignedInContext(Context, Options, signingInContext.Principal, signingInContext.Properties);         await Options.Events.SignedIn(signedInContext);     }      protected override async Task HandleSignOutAsync(SignOutContext context)     {         var signingOutContext = new BearerSigningOutContext(Context, Options);         await Options.Events.SigningOut(signingOutContext);         ApplyBearer(null);     }      protected override async Task&lt;AuthenticateResult&gt; HandleAuthenticateAsync()     {         var ticket = GetTicket();         if (ticket == null)             return AuthenticateResult.Failed(&quot;No ticket.&quot;);          var context = new BearerValidatePrincipalContext(Context, Options, ticket.Principal, ticket.Properties);         await Options.Events.ValidatePrincipal(context);         if (context.Principal == null)             return AuthenticateResult.Failed(&quot;No principal.&quot;);          if (context.ShouldRenew)             _shouldRenew = true;          return AuthenticateResult.Success(new AuthenticationTicket(context.Principal, context.Properties, Options.AuthenticationScheme));     }      protected override async Task&lt;bool&gt; HandleUnauthorizedAsync(ChallengeContext context)     {         Response.StatusCode = 401;          var unauthorizedContext = new BearerUnauthorizedContext(Context, Options);         await Options.Events.Unauthorized(unauthorizedContext);         return true;     }      protected override async Task&lt;bool&gt; HandleForbiddenAsync(ChallengeContext context)     {         Response.StatusCode = 403;          var forbiddenContext = new BearerForbiddenContext(Context, Options);         await Options.Events.Forbidden(forbiddenContext);         return true;     }      protected override async Task FinishResponseAsync()     {         if (!_shouldRenew || SignInAccepted || SignOutAccepted)             return;          var result = await HandleAuthenticateOnceAsync();         var ticket = result?.Ticket;         if (ticket == null)             return;          ApplyBearer(ticket);     } } <\/code><\/pre>\n<p>  \u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430:<\/p>\n<pre><code class=\"cs\">await HttpContext.Authentication.SignInAsync(BearerAuthenticationDefaults.AuthenticationScheme, principal); <\/code><\/pre>\n<p>  \u0433\u0434\u0435 principal \u2014 \u044d\u0442\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 ClaimsPrincipal, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0432 \u043c\u0435\u0442\u043e\u0434 HandleSignInAsync.<\/p>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043b\u0438\u0448\u044c \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u0434\u043e\u0431\u0430\u0432\u0438\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 \u0441\u0435\u0441\u0441\u0438\u0438 \u0432 ITicketStore.<\/p>\n<p>  \u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/itabaev\/bearer_authentication\">GitHub<\/a>.       <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/274163\/\"> http:\/\/habrahabr.ru\/post\/274163\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u041f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u043c\u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0435 ASP.NET WebApi \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Javascript \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0433\u043e API. \u0420\u0435\u0448\u0435\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 ASP.NET 5, \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0440\u0435\u043b\u0438\u0437.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0431\u044b \u044d\u0442\u043e \u0431\u044b\u043b\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0435 MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u044f \u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b cookie-based \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e, \u043d\u043e \u043a\u0440\u043e\u0441\u0441-\u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0443\u043a\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c token-based \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e.<\/p>\n<p>  Microsoft \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 JwtBearerAuthentication. \u041d\u043e \u043e\u0445\u043e\u0442\u0430 \u0436\u0435 \u0441\u0430\u043c\u043e\u043c\u0443 \u0432\u043e \u0432\u0441\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0440\u0435\u0448\u0438\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 BearerAuthentication.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-271266","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=271266"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/271266\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=271266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=271266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=271266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}