SignalR.NET客户端-分析值时遇到意外字符

SignalR .NET Client - Unexpected character encountered while parsing value(SignalR.NET客户端-分析值时遇到意外字符)

本文介绍了SignalR.NET客户端-分析值时遇到意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个.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();

集线器位于同一项目内-它是具有窗体身份验证的MVC应用程序。

我无法通过.Wait()调用,它总是出错,出现以下错误:

Message=Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
       Source=Newtonsoft.Json

更多跟踪:

在Newtonsoft.Json.JsonConvert.DeserializeObject[T](String值) 在Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b__1(String 原始) 在Microsoft.AspNet.SignalR.TaskAsyncHelper.<;>;c__DisplayClass192.<Then>b__17(Task1 t) 在Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners2.<>c__DisplayClass3a.<RunTask>b__39(Task1 t)

我有AppHub

public class AppHub : Hub { .. }

我做错了什么?

推荐答案

由于您使用的是窗体身份验证,而我在构造集线器连接时看不到您提供了任何凭据,这可能是您的问题所在。本页讨论如何使用窗体身份验证设置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;
        }
    }
}

远程登录页面:

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);
            }
        }
    }
}

这篇关于SignalR.NET客户端-分析值时遇到意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SignalR.NET客户端-分析值时遇到意外字符

基础教程推荐