我正在尝试设置一个.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
更多追踪:
at Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value) 在Microsoft.AspNet.SignalR.Client.Transports.TransportHelper.b_ 1(String 生的) 在Microsoft.AspNet.SignalR.TaskAsyncHelper。&lt;&gt; c _DisplayClass19
2.<Then>b__17(Task
1 T) 在Microsoft.AspNet.SignalR.TaskAsyncHelper.TaskRunners2.<>c__DisplayClass3a.<RunTask>b__39(Task
1 吨)
我有AppHub:
public class AppHub : Hub {
我做错了什么?
答案 0 :(得分:9)
由于您正在使用表单身份验证,并且在构建HubConnection时我看不到您提供任何凭据,这可能是您的问题。本页讨论如何使用Forms身份验证设置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;
}
}
}
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);
}
}
}
}
答案 1 :(得分:2)
作为问题的替代解决方案,您可能希望从需要身份验证的路径中排除Signalr:
<location path="signalr">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
答案 2 :(得分:0)
我在我的系统和代理服务器上运行代理应用程序返回Web请求的HTML页面!我关闭代理并工作!
答案 3 :(得分:0)
收到此消息时,是因为当指向服务器的URL时,我使用HTTP而不是HTTPS:
using (var hubConnection = new HubConnection("http://someurl.com"))
{
var hubProxy = hubConnection.CreateHubProxy("smsHub");
await hubConnection.Start();
await hubProxy.Invoke(....);
}
指向HTTPS URL解决了该问题。