【问题标题】:Issues with CurrentUserPropertyBinder it cannot always remember userCurrentUserPropertyBinder 的问题,它不能总是记住用户
【发布时间】:2013-10-03 13:38:55
【问题描述】:

我已经为使用 FubuMVC 的 Web 应用程序实现了 CurrentUserPropertyBinder(见下文)。

public class CurrentUserPropertyBinder : IPropertyBinder
    {
        private readonly Database _database;
        private readonly ISecurityContext _security;
        public CurrentUserPropertyBinder(Database database, ISecurityContext security)
        {
            _database = database;
            _security = security;
        }
        public bool Matches(PropertyInfo property)
        {
            return property.PropertyType == typeof(User)
                && property.Name == "CurrentUser";
        }
        public void Bind(PropertyInfo property, IBindingContext context)
        {
            var currentUser = //check database passing the username to get further user details using _security.CurrentIdentity.Name
            property.SetValue(context.Object, currentUser, null);
        }
    }

当我登录到我的网站时,一切正常。 CurrentUserPropertyBinder 包含执行任务所需的所有信息(即 _security.CurrentIdentity.Name 中包含正确的用户详细信息)

当我尝试使用打开标准 fileDialog 的 FineUploader (http://fineuploader.com/) 导入文件时,_security.CurrentIdentity.Name 为空。

它似乎不记得用户是谁,我不知道为什么。它适用于我的所有其他路线,但我导入了一个文件,它不会记住用户。

请帮忙!提前致谢

注意:我们使用 FubuMVC.Authentication 对用户进行身份验证

【问题讨论】:

  • 您是否使用 FubuMVC.Authentication 或其他方式来验证您的用户?
  • 我们正在使用 FubuMVC.Authentication 对用户进行身份验证

标签: fubumvc


【解决方案1】:

我猜你的行为被排除在身份验证之外;也许它是一个仅限 AJAX 的端点/动作。如果您在过去 3 个月左右更新了 FubuMVC.Authentication,我认为您可以在没有看到该操作是什么样子的情况下解决此问题。

您需要为此操作启用直通身份验证。开箱即用,FubuMVC.Auth 只为需要身份验证的操作连接 IPrincipal。如果您想从其他操作访问该信息,则必须启用传递过滤器。这里有一些快速的方法来做到这一点。

  1. 使用 [PassThroughAuthentication] 属性装饰您的端点/控制器类、此特定操作方法或此操作的输入模型,以选择加入传递身份验证。

    [PassThroughAuthentication]
    public AjaxContinuation post_upload_file(UploadInputModel input) { ... }
    

    [PassThroughAuthentication]
    public class UploadInputModel { ... }
    
  2. 在引导期间更改 AuthenticationSettings 以匹配 FubuRegistry 中的传递操作调用。

    ...
    AlterSettings<AuthenticationSettings>(x => {
        // Persistent cookie lasts 3 days ("remember me").
        x.ExpireInMinutes = 4320;
    
        // Many ways to filter here.
        x.PassThroughChains.InputTypeIs<UploadInputModel>();
    });
    

检查 /_fubu/endpoints 以确保带有您的操作调用的链应用了传递或身份验证过滤器。

【讨论】:

  • 谢谢 - 这是一个很好的答案,但我仍然遇到同样的问题。当我查看 /_fubu/endpoint 时,它仍在通过过滤器之前执行 ModelBinding。所以我仍然有同样的问题。我一定是做错了什么?
  • 快速笔记 - @ventaur - 除了我上面的评论,应该注意我的输入模型有我尝试绑定的属性。
  • 喂。这有点像第 22 条军规。我不肯定您可以在路由/输入绑定之前重新排序直通过滤器。我将为此请来大佬,以便我们为您提供最佳答案。
  • 谢谢您 - @ventaur 我会等待您的回复,再次感谢您迄今为止的所有帮助
  • 我在您在 Fubu 组的帖子中链接到了这个问题。我希望那里的知识比我多的人可以回答您。
猜你喜欢
  • 2017-03-03
  • 2023-04-01
  • 2014-11-09
  • 2013-04-09
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多