HttpContext.Current.User在与Asp.Net标识连接的OnConnect中为空
我用模板创建了两个项目。 第一个包含成员资格和MVC4。第二个使用MVC5和Asp.Net标识。 然后,我使用相同的代码将signalR添加到两个项目中。 在成员资格项目中,我可以访问控制器和信号器连接类(OnConnected方法)中的HttpContext.User。但在Identity项目中,我在控制器中有HttpContext.User的适当值。在OnConnected方法HttpContext中。用户返回null 两个项目的信号机代码相同: 1) SynchronizationConnection.cs 2) Startup.cs 3) Startup.Auth.csHttpContext.Current.User在与Asp.Net标识连接的OnConnect中为空,asp.net,asp.net-mvc,signalr,asp.net-mvc-5,asp.net-identity,Asp.net,Asp.net Mvc,Signalr,Asp.net Mvc 5,Asp.net Identity,我用模板创建了两个项目。 第一个包含成员资格和MVC4。第二个使用MVC5和Asp.Net标识。 然后,我使用相同的代码将signalR添加到两个项目中。 在成员资格项目中,我可以访问控制器和信号器连接类(OnConnected方法)中的HttpContext.User。但在Identity项目中,我在控制器中有HttpContext.User的适当值。在OnConnected方法HttpContext中。用户返回null 两个项目的信号机代码相同: 1) SynchronizationConn
由于缺少[Authorize]属性,我看到了有关null User.Identity.Name的问题。在我的情况下,我甚至无法访问User.Identity。另外,我的操作上有[Authorize]属性,其中包含客户端javascript。在
OnConnected
方法中,您可以使用:
request.User
这将携带连接时在HttpContext.Current.User中的IPrincipal
,这是您应该注意的
如果需要更改该IPrincipal
(例如删除表单身份验证生成的默认值并设置自己的),我建议您使用IHttpModule
。SignalR中的所有传输至少以HTTP(甚至WebSockets)开始,因此所有传输都将至少在连接期间命中模块
干杯。我遇到了同样的问题:request.User在PersistentConnection的OnConnected()方法中为null。更改初始化信号器的顺序,我的身份验证提供程序修复了它。我对其进行了更改,以便在配置身份验证后映射我的信号器端点:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = new TimeSpan(999, 0, 0, 0),
SlidingExpiration = true
});
GlobalConfiguration.Configuration.UseSqlServerStorage("MyDatabase", new SqlServerStorageOptions() { PrepareSchemaIfNecessary = false });
app.MapSignalR<MyConnection>("/MyConnection");
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
ExpireTimeSpan=新的时间跨度(999,0,0,0),
slidengexpiration=true
});
GlobalConfiguration.Configuration.UseSqlServerStorage(“MyDatabase”,新的SqlServerStorageOptions(){PrepareSchemaIfEssential=false});
附录MapSignalR(“/MyConnection”);
我已尝试请求。首先是用户。它也是空的。对不起,我说的不准确。你是怎么解决的?
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.MapSignalR<Services.Realtime.SynchronizationConnection>("/test");
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
var connection = $.connection('/test');
connection.logging = true;
console.log('Receiving connection');
connection.received(function (data) {
console.log('received');
});
connection.disconnected(function () {
console.log('disconnected');
});
connection.error(function (data) {
console.log('error');
});
connection.start().done(function () {
console.log('Connection started');
});
request.User
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
ExpireTimeSpan = new TimeSpan(999, 0, 0, 0),
SlidingExpiration = true
});
GlobalConfiguration.Configuration.UseSqlServerStorage("MyDatabase", new SqlServerStorageOptions() { PrepareSchemaIfNecessary = false });
app.MapSignalR<MyConnection>("/MyConnection");