一个控制器内SignalR认证? [英] SignalR authentication within a Controller?

查看:491
本文介绍了一个控制器内SignalR认证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩SignalR,我似乎无法把握验证来建立一个演示应用程序供市民VS安全聊天。有一个聊天室,我想证明身份验证的用户将获得公共留言的的身份验证的用户信息。认证是使用股票MVC(3)的AccountController 互联网应用程序完成的。

获取控制器内的中心范围内没有意义,因为它不具有连接ID。我怎样才能获得的连接ID添加到组的特定连接安全聊天?如果它在控制器内完成?还是我失去了一些方法来毂内呢?

  [HttpPost]
公众的ActionResult登录(LogOnModel型号,串RETURNURL)
{
    如果(ModelState.IsValid)
    {
        如果(Membership.ValidateUser(model.UserName,model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);            VAR背景= GlobalHost.ConnectionManager.GetHubContext<聊天和GT;();
            //增加来signalr安全组
            //但没有连接ID在这里


解决方案

我的完全的俯瞰明显,有据可查。一旦用户被认证为正常,中心派生类就可以确认身份验证例如

 公共类聊天:集线器
{
    公共无效发送(字符串消息)
    {
        Clients.addMessage(消息);
    }    公共无效securesend(字符串消息)
    {
        如果(this.Context.User.Identity.IsAuthenticated)
        {
            //可能还不是安全组的一部分
            Groups.Add(this.Context.ConnectionId,担保);
            。客户端[担保]方法addMessage(担保[]+消息);
        }
    }
}

I'm playing with SignalR, and I can't seem to grasp authentication to build a demo app for public vs secure chat. There is one chat room, and I want to demonstrate that authenticated users will receive public messages and authenticated user messages. Authentication is done using the stock MVC(3) Internet app in AccountController.

Getting a Hub context within the controller doesn't make sense, since it doesn't have the connection id. How can I get the connection id to add the specific connection to a group for 'secure chat?' Should it be done in the controller? Or am I missing some way to do it within the hub?

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

            var context = GlobalHost.ConnectionManager.GetHubContext<Chat>();
            // add to signalr secure group
            // but no connection id here

解决方案

I was completely overlooking the obvious and well documented. Once the user is authenticated as normal, the Hub-derived class will be able to confirm authentication e.g.

public class Chat : Hub
{
    public void send(string message)
    {
        Clients.addMessage(message);
    }

    public void securesend(string message)
    {
        if (this.Context.User.Identity.IsAuthenticated)
        {
            // might not yet be a part of the "secured" group
            Groups.Add(this.Context.ConnectionId, "secured");
            Clients["secured"].addMessage("[SECURED] " + message);
        }
    }
}

这篇关于一个控制器内SignalR认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆