SignalR .Net Client - 解析值时遇到意外的字符

Rob*_*ous 6 signalr signalr-hub

我正在尝试建立一个.Net客户端,从我的服务层向我的signalR集线器发送消息.我正在关注本指南:http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client#callserver

这就是我所拥有的:

        _hubConnection = new HubConnection(_baseUrl); // "http://localhost:3806"
        _hubProxy = _hubConnection.CreateHubProxy("AppHub");
        _hubConnection.Start().Wait();
Run Code Online (Sandbox Code Playgroud)

该集线器位于同一个项目中 - 它是一个带有表单身份验证的MVC应用程序.

我永远无法通过.Wait()调用,它总是出错以下内容:

Message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
       Source=Newtonsoft.Json
Run Code Online (Sandbox Code Playgroud)

更多痕迹:

在Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b_中的Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值).在Microsoft.AspNet.SignalR.TaskAsyncHelper上的字符串原始字符串.<> c _DisplayClass19 2.<Then>b__17(Task1 t)在Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners 2.<>c__DisplayClass3a.<RunTask>b__39(Task1 t)

我有AppHub:

public class AppHub : Hub {
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Hal*_*eth 10

由于您使用的是表单身份验证,并且在构建HubConnection时我看不到您提供任何凭据,这可能是您的问题.本页讨论如何使用表单身份验证设置SignalR连接:http: //www.asp.net/signalr/overview/signalr-20/security/hub-authorization

class Program
    {
    static void Main(string[] args)
    {
        var connection = new HubConnection("http://www.contoso.com/");
        Cookie returnedCookie;

        Console.Write("Enter user name: ");
        string username = Console.ReadLine();

        Console.Write("Enter password: ");
        string password = Console.ReadLine();

        var authResult = AuthenticateUser(username, password, out returnedCookie);

        if (authResult)
        {
            connection.CookieContainer = new CookieContainer();
            connection.CookieContainer.Add(returnedCookie);
            Console.WriteLine("Welcome " + username);
        }
        else
        {
            Console.WriteLine("Login failed");
        }    
    }

    private static bool AuthenticateUser(string user, string password, out Cookie authCookie)
    {
        var request = WebRequest.Create("https://www.contoso.com/RemoteLogin") as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CookieContainer = new CookieContainer();

        var authCredentials = "UserName=" + user + "&Password=" + password;
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(authCredentials);
        request.ContentLength = bytes.Length;
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);
        }

        using (var response = request.GetResponse() as HttpWebResponse)
        {
            authCookie = response.Cookies[FormsAuthentication.FormsCookieName];
        }

        if (authCookie != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

RemoteLogin页面:

using System;
using System.Web.Security;

namespace SignalRWithConsoleChat
{
    public partial class RemoteLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request["UserName"];
            string password = Request["Password"];
            bool result = Membership.ValidateUser(username, password);
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username, false);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 当集线器为“授权”但在 connection.Start() 时未提供凭据或凭据不正确时,会发生此错误,但这不是一条非常有用的消息 (2认同)