SignalR .Net Client - Unexpected character encountered while parsing value

后端 未结 4 1261
遥遥无期
遥遥无期 2021-02-09 12:33

I\'m trying to set up a .Net client to send messages to my signalR hub from my service layer. I\'m following this guide: http://www.asp.net/signalr/overview/signalr-20/hubs-api

相关标签:
4条回答
  • 2021-02-09 13:12

    As an alternative solution to the problem, you may want to exclude Signalr from the paths which require authentication:

    <location path="signalr">
      <system.web>
        <authorization>
          <allow users="*" />
        </authorization>
      </system.web>
    </location>
    
    0 讨论(0)
  • 2021-02-09 13:22

    i running proxy application on my system and proxy server return a HTML page for web requests! i turn off proxy and works!

    0 讨论(0)
  • 2021-02-09 13:26

    Since you are using Forms Authentication and I cannot see that you are providing any credentials when you construct your HubConnection, that might be your problem. This page talks of how to setup a SignalR connection using Forms Authentication: 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 page:

    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);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-09 13:30

    When I was getting this message, it was because when pointing to the URL of the server I used HTTP instead of HTTPS:

    using (var hubConnection = new HubConnection("http://someurl.com"))
    {
        var hubProxy = hubConnection.CreateHubProxy("smsHub");
        await hubConnection.Start();
        await hubProxy.Invoke(....);
    }
    

    Pointing to the HTTPS url resolved the issue.

    0 讨论(0)
提交回复
热议问题