Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Builder;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10.  
  11. namespace _3._7
  12. {
  13.     public class Startup
  14.     {
  15.         // This method gets called by the runtime. Use this method to add services to the container.
  16.         // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  17.         public void ConfigureServices(IServiceCollection services)
  18.         {
  19.             services.AddDistributedMemoryCache();
  20.             services.AddSession();
  21.         }
  22.  
  23.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  24.  
  25.         public interface IMessageSender
  26.         {
  27.             string Request(HttpContext contex, string text);
  28.             void Send(HttpContext context, string key, string value);
  29.         }
  30.  
  31.         class EmailMessageSender : IMessageSender // Сессии
  32.         {
  33.             public string Request(HttpContext context, string key)
  34.             {
  35.                 var s = "EMPTY";
  36.                 if (context.Session.Keys.Contains(key))
  37.                 {
  38.                     s = context.Session.GetString(key);
  39.                 }
  40.  
  41.                 return s;
  42.             }
  43.  
  44.             public void Send(HttpContext context, string key, string value)
  45.             {
  46.                 context.Session.SetString(key, value);
  47.             }
  48.         }
  49.  
  50.         public class SmsMessageSender : IMessageSender // Куки
  51.         {
  52.             public string Request(HttpContext context, string key)
  53.             {
  54.                 string s;
  55.                 if (context.Request.Cookies.TryGetValue(key, out s))
  56.                 {
  57.                     return s;
  58.                 }
  59.  
  60.                 return "EMPTY";
  61.             }
  62.  
  63.             public void Send(HttpContext context, string key, string value)
  64.             {
  65.                 context.Response.Cookies.Append(key, value);
  66.             }
  67.         }
  68.  
  69.         public class MessageService
  70.         {
  71.             public void Send(IMessageSender Im, HttpContext context, string key, string value)
  72.             {
  73.                 Im.Send(context, key, value);
  74.             }
  75.         }
  76.  
  77.  
  78.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  79.         {
  80.             app.UseSession();
  81.  
  82.             IMessageSender email = new EmailMessageSender(); //session
  83.             IMessageSender sms = new SmsMessageSender(); //cookies
  84.  
  85.             var msg = new MessageService();
  86.  
  87.             string s = "111";
  88.             string key = s;
  89.             string value = s;
  90.  
  91.             app.Run(async (context) =>
  92.             {
  93.                 var emailString = email.Request(context, key);
  94.                 var smsString = sms.Request(context, key);
  95.  
  96.                 await context.Response.WriteAsync(emailString + '\n' + smsString);
  97.  
  98.                 msg.Send(email, context, key, value);
  99.                 msg.Send(sms, context, key, value);
  100.                
  101.             });
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement