SignalR .NET Client - Unexpected character encountered while parsing value(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__DisplayClass19
2.<Then>b__17(Task
1 t) 在Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners2.<>c__DisplayClass3a.<RunTask>b__39(Task
1 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客户端-分析值时遇到意外字符
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01