- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 MVC 应用程序,它带有自定义主体的基于表单的身份验证。在使用应用程序之前用户必须登录。在此之后我想使用 SignalR,问题是 Context.User.Identity.Name 总是空字符串。
自定义主体.cs
public class CustomPrincipal : IPrincipal
{
public CustomPrincipal(IIdentity identity)
{
Identity = identity;
}
public IIdentity Identity { get; }
public bool IsInRole(string role)
{
return true;
}
}
public class CustomIdentity : IIdentity
{
public CustomIdentity(EmployeeModel user)
{
Name = user.Username;
Id = user.Id;
}
public string AuthenticationType => "Custom";
public bool IsAuthenticated => !string.IsNullOrEmpty(Name);
public int Id { get; set; }
public string Name { get; }
}
protected override void OnAuthorization(AuthorizationContext context)
{
if (SessionPersister.User != null && !string.IsNullOrEmpty(SessionPersister.User.Username))
{
context.HttpContext.User = new CustomPrincipal(new CustomIdentity(SessionPersister.User));
}
base.OnAuthorization(context);
}
public override Task OnConnected()
{
string name = Context.User.Identity.Name; // it's empty
return base.OnConnected();
}
最佳答案
所以,轻微的逻辑错误:BaseController.OnAuthorization
仅在执行 Controller 时触发。当 SignalR 请求通过时,将永远不会为该请求调用该方法。
因此,一种解决方法是将代码从 Controller 移动到更全局的范围。例如,您可以使用 Global.asax.cs
并添加它,就像这样:
protected void Application_PostAuthenticateRequest( object sender, EventArgs e )
{
//do your custom principal setting here.
this.Context.User = new CustomPrincipal( new CustomIdentity( 10, "test" ) );
}
public String Hello(String hello)
{
//no need to actually cast if you don't need the non-iidentity properties
//var identity = (CustomIdentity) this.Context.User.Identity;
//identity.Id == 10
//identity.Name == "test"
return hello;
}
关于authentication - 将 IPrincipal 从 MVC 传递到 SignalR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924027/
我想模拟 IPrincipal 所以我这样做了 public Mock Principal { get; set; } 在我的 nunit 设置中 Principal = new Mock(); 所
我需要了解 SiteMapProvider.IsAccessibleToUser() 的工作原理。 内置 XmlSiteMapProvider 调用 HttpContext.User.IsInRole
是否可以使用 CaSTLe Windsor 将 IPrincipal 注入(inject)到我的 asp.net mvc Controller 中。本文作者Scott Hanselman评论中有代码可
我需要通过 Directory.GetDirectories() 和 Directory.GetFiles() 方法访问当前 IPrincipal 有权访问的文件和目录,而不列出其他文件。该进程本身作
我在 ASP.net MVC 应用程序中使用 Windows 身份验证。我想查询 Active Directory 以获取基于当前用户的用户电子邮件地址: IPrincipal principal =
我已经阅读了很多关于这个主题的问题和答案,但没有一个能帮助我解决这个问题。 我遇到的问题是 HttpContext.Current.User 或只是 User 属性的类型是 RolePrincipal
有没有办法实现下一个行为? public static void Configure(IServiceCollection services) { services.AddScoped(typ
伙计们,我不包括 ATM 代码,因为这似乎是 Azure B2C 设置问题,而不是编码问题。如果需要,我会上传它,但有问题的代码需要阅读很多。 问:是否有人成功从 Azure B2C“预览”租户迁移到
我正在 global.asax 中使用 Application_PostAuthenticateRequest 事件来创建自定义 IPrincipal 对象 void Application_Post
我正在实现一个我想在多个应用程序中使用的自定义 IPrincipal。我有 2 个关于 IsInRole 方法的问题... 1) 是否建议我将自定义 RoleProvider 与自定义 IPrinci
我正在尝试在我当前的 winforms 项目中实现授权和身份验证。身份验证还必须与 SQL Server 2008 数据库中的用户相匹配。问题是,它是一个多用户程序,所以当添加新用户时,会创建一个数据
我可以在他使用WCF服务时获取请求者窗口IPrincipal吗? 最佳答案 使用此代码,您可以检查 WCF 服务中的当前安全上下文。 如果用户已通过身份验证,并且 WindowsPrincipal 不
我正在使用 LINQPad 测试代码(我必须说这是一个多么棒的产品)但是现在当我尝试将 Thread.CurrentPrincipal 设置为标有 SerializableAttribute 的自定义
我有一个严重依赖用户授权的应用程序。在其中,我使用 IPrincipal.IsInRole() 检查用户是否在正确的组中: IPrincipal principal = Thread.CurrentP
我想在 asp.net 中扩展 IPrincipal 以允许我获得我将定义的用户类型。我想在 Controller 中实现这一点 string type = User.UserType 然后在我的扩展
因为 IPrincipal 具有 Identity 属性,在我的 User 类上实现这两个接口(interface)是否很好? 最佳答案 通常,用户和用户交互的安全上下文是不同的东西,因此如果以通用方
我假设 MembershipUser 对象和实现 IPrincipal 接口(interface)的对象在某种意义上是“连接的”,即当其中一个对象中的某些信息发生变化时,另一个对象也会相应地发生变化?
这个问题在这里已经有了答案: Can I access IIdentity from Web API (4 个回答) 8年前关闭。 我需要在我的操作过滤器中访问当前登录的用户 .身份由 Delegat
几天来我一直在寻找这个问题的答案,但我没有找到任何成功。我会发布链接,但它可能会占用整个页面。 所以这就是我所拥有的...... 我有一个 MVC 应用程序,它使用 WC-Federation 协议(
场景 我正在使用自定义IPrincipal和IIdentity进行asp.net授权。我设置了Principal和Identity期间PostAuthenticateRequest使用 IHttpMo
我是一名优秀的程序员,十分优秀!