Шифрование потока

191
28 марта 2018, 02:04

Делаю чат (клиент и сервер), при старте сервера пытаюсь зашифровать соединение через сертификат

Код Program.cs

class Program
{
    public X509Certificate2 cert = new X509Certificate2("server.pfx", "enote");
    static void Main(string[] args)
    {
        var url = "http://192.168.0.61:8080/";
        using (WebApp.Start<Startup>(url))
        {
            Console.WriteLine($"Статус: Сервер запущен. Адрес: {url}");
            Console.ReadLine();
        }
    }
}
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR("/enote", new HubConfiguration());
    }
}

Подскажите, пожалуйста, как я могу это сделать?

Код IClient.cs

public interface IClient
{
    void ParticipantDisconnection(string name);
    void ParticipantReconnection(string name);
    void ParticipantLogin(User client);
    void ParticipantLogout(string name);
    void BroadcastMessage(string sender, string message);
    void UnicastMessage(string sender, string message);
}

Код ChatHub.cs

public class ChatHub : Hub<IClient>
{
    private static ConcurrentDictionary<string, User> ChatClients = new ConcurrentDictionary<string, User>();
    public override Task OnDisconnected(bool stopCalled)
    {
        var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
        if (userName != null)
        {
            Clients.Others.ParticipantDisconnection(userName);
            Console.WriteLine($"<> {userName} отключен");
        }
        return base.OnDisconnected(stopCalled);
    }
    public override Task OnReconnected()
    {
        var userName = ChatClients.SingleOrDefault((c) => c.Value.ID == Context.ConnectionId).Key;
        if (userName != null)
        {
            Clients.Others.ParticipantReconnection(userName);
            Console.WriteLine($"== {userName} переподключен");
        }
        return base.OnReconnected();
    }
    public List<User> Login(string name, string password, byte[] photo)
    {
        if (!ChatClients.ContainsKey(name))
        {
            Console.WriteLine($"++ {name} выходит в сеть");
            List<User> users = new List<User>(ChatClients.Values);
            User newUser = new User { Name = name, Password = password, ID = Context.ConnectionId, Photo = photo };
            var added = ChatClients.TryAdd(name, newUser);
            if (!added) return null;
            Clients.CallerState.UserName = name;
            Clients.Others.ParticipantLogin(newUser);
            return users;
        }
        return null;
    }
    public void Logout()
    {
        var name = Clients.CallerState.UserName;
        if (!string.IsNullOrEmpty(name))
        {
            User client = new User();
            ChatClients.TryRemove(name, out client);
            Clients.Others.ParticipantLogout(name);
            Console.WriteLine($"-- {name} ушел из сети");
        }
    }
    public void BroadcastChat(string message)
    {
        var name = Clients.CallerState.UserName;
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(message))
        {
            Clients.Others.BroadcastMessage(name, message);
        }
    }
    public void UnicastChat(string recepient, string message)
    {
        var sender = Clients.CallerState.UserName;
        if (!string.IsNullOrEmpty(sender) && recepient != sender &&
            !string.IsNullOrEmpty(message) && ChatClients.ContainsKey(recepient))
        {
            User client = new User();
            ChatClients.TryGetValue(recepient, out client);
            Clients.Client(client.ID).UnicastMessage(sender, message);
        }
    }
}
READ ALSO
Зависла установка monodevelop Mac OS

Зависла установка monodevelop Mac OS

Ввожу команды по инструкции с monodevelop

187
Как сделать массив double nullable

Как сделать массив double nullable

Как сделать массив double nullable?

199
Заполнить combobox данными из запроса (mvvm)

Заполнить combobox данными из запроса (mvvm)

Данный запрос наполняет DataGridМожно ли как то из этого же запроса наполнить combobox значениями колонки Manager_History

201
Парсинг WORD-документов

Парсинг WORD-документов

В общем, есть Word-документы формата *doc

194