Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JS客户端的信号器控制台应用程序上进行身份验证_Javascript_C#_Asp.net_Angularjs_Signalr - Fatal编程技术网

Javascript 在JS客户端的信号器控制台应用程序上进行身份验证

Javascript 在JS客户端的信号器控制台应用程序上进行身份验证,javascript,c#,asp.net,angularjs,signalr,Javascript,C#,Asp.net,Angularjs,Signalr,以下场景/我的解决方案包括以下内容: 项目一:(SELF-HOST)我有一个signar控制台应用程序,它处理包括身份验证过程(使用EF查询数据库)在内的逻辑。项目二:(CLIENT)我有一个ASP.Net web应用程序和一个AngularJS客户端 到目前为止,我可以和中心谈得很好。问题是,我似乎无法使身份验证正常工作。我已经尝试了很多我发现的东西,但是没有一个奏效。他们中的大多数人甚至不适用于我的问题 目前,我已将我的项目剥离到基本内容,我有以下代码: 启动类: public class

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

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

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

目前,我已将我的项目剥离到基本内容,我有以下代码:

启动类:

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

你把密码搞砸了。试试这个:

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

Startup.cs
中,您需要添加表单身份验证中间件(可能需要对其进行一些调整):


我最近遇到了类似的问题。如果我没弄错的话,您希望在Signal server应用程序上执行身份验证。signer可以接受标准的webrequest

将authenticationtype设置为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);
                        }
                    }
如果您想为登录页面提供服务,可以这样做(\u Authpage是您的页面字符串,例如)

如果用户还需要其他内容(例如authpage中的其他样式文件)


所有这些都属于你的创业公司。

?连接正常。我只是不知道如何验证。那不在我的范围之内。你退房了吗?是的。没有js客户端的示例。您使用的是哪种身份验证?目前没有。这就是问题所在。理想情况下,我希望以某种方式通过我的中心进行身份验证。(请查看
Authenticate()
)我一直在其他项目上使用FormsAuthentication,但它们都是带有SignalR的ASP.Net项目。这一次,信号器部件是自托管的。我在谷歌上搜索过,但我不知道从哪里开始。你有没有尝试过简单的公式化?信号器应该可以很好地处理身份验证cookie。因此,在这里,在public-void-Authenticate(String-pLogin,String-pPassword)中执行FormsAuthentication.SetAuthCookie(username,false);我不知道这在控制台环境中是如何工作的。我不知道我可以像那样为网页服务。Signaler是否只通过浏览URL就知道该做什么?我需要在其他地方注册吗?如果没有,这看起来很可靠。是的,你可以。您不需要在其他任何地方注册它们。至少我不知道你在哪里,是否能做到。我还没有真正研究过这个问题,因为我只需要提供auth页面。
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);
                        }
                    }
                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);
            }
                else
            {
                await next();
            }