JavaScript Authenticate on SignalR console application from JS client

Following scenario/my solution consists of the following: 

Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries database with EF ).
Project two: (CLIENT) I have an ASP.Net web application with an AngularJS client. 

So far I can talk to the hub just fine. The problem is, I cannot seem to get the authentication to work. I've tried a bunch of things I've found but none of them worked. Most of them didn't even apply to my problem.. 

Currently I've stripped my project back to the basics and I have the following code:

Startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.MapSignalR();
    }
}

My hub:

[HubName("systemHub")]
public class systemHub : Hub
{

    public void Authenticate(String pLogin, String pPassword)
    {
        User curUser = new AuthManager().Authenticate(pLogin, pPassword);
//this is where I'd want to send the auth cookie or whatever and contact the "loginCallback" function in my client
    }

    [Authorize]
    public void Hello(String pMessage)
    {
        Clients.All.callbackFunc(pMessage);
    }
}

Js client:

    hinagApp.controller('hinagController', function ($scope) {
    $(document).ready(function () {
        var conURL = 'http://localhost:8080/signalr';
        $.getScript(conURL + '/hubs', function () {
            $.connection.hub.url = conURL;
            var lHub = $.connection.systemHub;

            lHub.client.callbackFunc = function(pM){
                alert(pM);
            }

            lHub.client.loginCallback = function (pSuccess) {
                if (pSuccess) {
                    //if logged in
                    lHub.server.hello("test");
                }
                else {
                    alert("fail");
                }
            }

            $('#loginbutton').click(function () {
                lHub.server.authenticate($('#input_login').val(), $('#input_pass').val());
            });

            $.connection.hub.start();
        });
    })
});
Answer:1

I recently ran into a similar problem. If I understand you right, you want to do the authentication on your signalr server application. Signalr can accept standard webrequests just fine.

Set the authenticationtype to cookies:

        CookieAuthenticationOptions lOptions = new CookieAuthenticationOptions()
        {
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            LoginPath = new PathString("/Auth/Login"),
            LogoutPath = new PathString("/Auth/Logout"),
        };

        app.UseCookieAuthentication(lOptions);

If user wants to login, set the claims you'd like to use

var lForm = await context.Request.ReadFormAsync();
                    if (!String.IsNullOrEmpty(lForm["input_login"]) && !String.IsNullOrEmpty(lForm["input_pass"]))
                    {
                        //Benutzer authentifizieren
                        var lAuthenticatedUser = new UserManager().Authenticate(lForm["input_login"], lForm["input_pass"]);
                        if (lAuthenticatedUser != null)
                        {
                            //Existiert der Nutzer legen wir die Claims an
                            ClaimsIdentity lIdentity = new ClaimsIdentity(lOptions.AuthenticationType);
                            lIdentity.AddClaim(new Claim(ClaimTypes.Name, lAuthenticatedUser.Username));
                            lIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, lAuthenticatedUser.InternalUserId.ToString()));
                            lIdentity.AddClaim(new Claim(ClaimTypes.SerialNumber, context.Request.RemoteIpAddress));

                            //Und zum Schluss einloggen
                            context.Authentication.SignIn(lIdentity);

                            //Und auf die Spieleseite weiterleiten
                            context.Response.Redirect(BLL._Configuration.HinagGameURL);
                        }
                    }

If you want to serve the login page you can do it like this (_Authpage is your page as String, for example)

                else if (context.Request.Path.Value == "/Auth/")
            {
                if (context.Authentication.User != null)
                    context.Response.Redirect(BLL._Configuration.HinagGameURL);


                context.Response.ContentType = "text/html";
                await context.Response.WriteAsync(_Authpage);
            }

If the user needs anything else ( such as additional style files in your authpage )

                else
            {
                await next();
            }

All of this belongs in your Startup.

Answer:2

In Startup.cs you need to add forms authentication middleware (probably you need to tune it a bit):

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
});

https://msdn.microsoft.com/en-us/library/microsoft.owin.security.cookies.cookieauthenticationoptions(v=vs.113).aspx

Answer:3

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

  1. file with node
  2. file upload with node js
  3. download file with node
  4. run file with node
  5. read file with node
  6. create file with node
  7. download file with node js
  8. read file with node js
  9. create file with node js
  10. open file with node.js
  11. delete file with node js
  12. write file with node js
  13. write file with node
  14. delete file with node
  15. upload file with node
  16. file upload with node and react
  17. open file with node
  18. file server with node.js
  19. node dockerfile
  20. send file with node js

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...

I'm debugging someone's code and wondering what this regex does? Code: <script> var t = document.getElementById("filterVal").value; var s = filterVal.replace(/"([^"]+(?="))"/g, '$1') </...

I'm debugging someone's code and wondering what this regex does? Code: <script> var t = document.getElementById("filterVal").value; var s = filterVal.replace(/"([^"]+(?="))"/g, '$1') </...

  1. what does this emoji mean
  2. what does this mean �� ��
  3. what does this symbol mean
  4. what does this emoji mean ��
  5. what does this sign mean
  6. what does this mean in english
  7. what does this word mean
  8. what does this emoji mean ��
  9. what does this emoji mean ��
  10. what does this emoji mean ��
  11. what does this emoji mean ��
  12. what does this mean in texting
  13. what does this national emergency mean
  14. what does this emoji mean ��
  15. what does this symbol mean in math
  16. what does this quote mean
  17. what does this mean in spanish
  18. what does this mean in math
  19. what does this mean 3
  20. what does this emoji mean ��