【问题标题】:Authenticate on SignalR console application from JS client从 JS 客户端对 SignalR 控制台应用程序进行身份验证
【发布时间】:2015-11-16 20:36:14
【问题描述】:

以下场景/我的解决方案包括以下内容:

项目一:(SELF HOST) 我有一个SignalR 控制台应用程序,它处理包括身份验证过程在内的逻辑(使用EF 查询数据库)。
项目二:(CLIENT) 我有一个ASP.Net web applicationAngularJS 客户端。

到目前为止,我可以很好地与中心交谈。问题是,我似乎无法让身份验证工作。我已经尝试了很多我发现的东西,但没有一个有效。他们中的大多数甚至都不适用于我的问题..

目前我已经将我的项目回归基础,并且我有以下代码:

启动类:

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

我的中心:

[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 客户端:

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

【问题讨论】:

  • 是的。没有js客户端的例子。
  • 您使用的是哪种身份验证?
  • 没有,到目前为止。那就是问题所在。理想情况下,我想以某种方式通过我的集线器进行身份验证。 (请看Authenticate())我一直在其他项目上使用FormsAuthentication,但它们都是带有SignalR的ASP.Net项目。这次 SignalR 部分是自托管的。我已经用谷歌搜索了,但我就是不知道从哪里开始。
  • 您是否尝试过简单地进行 FormsAuthentification? SignalR 应该与身份验证 cookie 一起正常工作。所以在这里,在 public void Authenticate(String pLogin, String pPassword) 做 FormsAuthentication.SetAuthCookie(username, false);

标签: javascript c# asp.net angularjs signalr


【解决方案1】:

我最近遇到了类似的问题。如果我理解你的正确,你想对你的信号服务器应用程序进行身份验证。 Signalr 可以接受标准的网络请求就好了。

将身份验证类型设置为cookies:

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

        app.UseCookieAuthentication(lOptions);

如果用户想登录,请设置您想使用的声明

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

如果你想提供登录页面,你可以这样做(例如,_Authpage 是你的页面作为字符串)

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

如果用户需要其他任何东西(例如您的 authpage 中的其他样式文件)

                else
            {
                await next();
            }

所有这些都属于你的 Startup。

【讨论】:

  • 我没有意识到我可以像那样提供网页服务。 SignalR 是否仅通过浏览 URL 来识别要做什么?我需要在其他地方注册吗?如果没有,这似乎很可靠。
  • 是的,你可以。您无需在其他任何地方注册它们。至少我不知道在哪里以及是否可以这样做。我还没有真正研究过,因为我只需要提供身份验证页面。
【解决方案2】:

Startup.cs中你需要添加表单认证中间件(可能你需要稍微调整一下):

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

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

【讨论】:

  • 我看不出这在控制台环境中是如何工作的。
【解决方案3】:

你用那个代码搞砸了 Angular。试试这个:

hinagApp
    .controller('hinagController', function ($scope, $http) {
    var conURL = 'http://localhost:8080/signalr';
    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");
        }
    }

    $http
        .get(conURL + '/hubs')
        .then(function(response) {
            $.connection.hub.url = conURL;
            $('#loginbutton').click(function () {
                lHub.server.authenticate($('#input_login').val(), $('#input_pass').val());
            });

            $.connection.hub.start();
        });
});

【讨论】:

  • 嗯?连接工作正常。我只是不知道如何进行身份验证。
  • 这超出了我的范围。
猜你喜欢
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2016-04-24
  • 1970-01-01
相关资源
最近更新 更多