将权限/身份验证复制到子线程...?

Posted

技术标签:

【中文标题】将权限/身份验证复制到子线程...?【英文标题】:Copy permissions / authentication to child threads...? 【发布时间】:2012-05-17 19:10:04 【问题描述】:

这是我注意到的一件非常奇怪的事情。

我正在编写一个 CRM 2011 Silverlight 扩展,而且,在我的本地开发实例上一切正常。应用程序使用OData进行通信,并大量使用System.Threading.Tasks.Task在后台执行所有操作(FromAsync是一种祝福)。

但是,我决定在 CRM 2011 Online 中测试我的应用程序,但令我惊讶的是,它不再工作了。结束检索任务时我会收到安全异常。

使用 Fiddler,我发现 CRM 试图将我重定向到实时登录页面,考虑到我已经登录,这没有多大意义。

经过多次尝试,我发现错误是因为我从与 UI 线程不同的线程访问服务。

这是一个简单的例子:

    //this will work
    private void button1_Click(object sender, RoutedEventArgs e)
    
        var query = ctx.AccountSet;
        query.BeginExecute((result) =>
        
            textBox1.Text = query.EndExecute(result).First().Name;
        , null);
    

    //this will fail
    private void button2_Click(object sender, RoutedEventArgs e)
    
        System.Threading.Tasks.Task.Factory.StartNew(RestAsync);
    

    void RestAsync()
    
        var query = ctx.AccountSet;
        var async = query.BeginExecute(null, null);
        var task = System.Threading.Tasks.Task.Factory.FromAsync<Account>(async, (result) =>
        
            return query.EndExecute(result).First(); // <- Exception thrown here
        );
        textBox1.Dispatcher.BeginInvoke(() =>
        
            textBox1.Text = task.Result.Name;
        );
    

似乎很明显我缺少一些关于线程如何使用权限的基础知识。由于在我的情况下最好使用单独的线程,有没有办法“复制”权限/身份验证?也许是某种模仿?

编辑:如果其他人为此苦苦挣扎,只要在 UI 线程上执行query.BeginExecute(null, null);,就可以使用其他线程(或Task,视情况而定)。您需要一种将返回的IAsyncResult 检索回调用线程的方法,但您可以使用ManualResetEvent 来实现。

但我仍然想知道为什么线程之间不共享该死的权限/身份验证...

【问题讨论】:

可能和current thread's Execution Context有关。 很有可能,但是我想指出,在本地、内部部署的 CRM 服务器上测试我的代码时,一切正常。因此,目前还不清楚到底发生了什么。 【参考方案1】:

我不太确定,这是否会有所帮助。但我发现了 Jeffrey Richter 第 770 页的描述

“与控制台应用程序一样,ASP.NET Web 窗体和 XML Web 服务应用程序允许 任何线程做任何它想做的事。当一个线程池线程开始处理客户端的 请求,它可以假设客户的文化(System.Globalization.CultureInfo),允许 Web 服务器返回数字、日期和时间的特定于文化的格式。5 在 此外,Web 服务器可以假设客户端的身份(System.Security.Principal. IPrincipal) 以便服务器只能访问允许客户端访问的资源 使用权。当一个线程池线程产生一个异步操作时,它将被完成 由另一个线程池线程处理异步操作的结果。 虽然这项工作是代表原始客户请求执行的,但文化 并且身份信息默认不会流向新的线程池线程,所以任何 代表客户完成的额外工作现在没有使用客户的文化和身份 信息。理想情况下,我们希望文化和身份信息流向另一个线程 池仍在代表同一个客户端工作的线程。”

这是他的例子,我希望这会有所帮助。

private static AsyncCallback SyncContextCallback(AsyncCallback callback) 

  SynchronizationContext sc = SynchronizationContext.Current;
  // If there is no SC, just return what was passed in
  if (sc == null) return callback;
  // Return a delegate that, when invoked, posts to the captured SC a method that
  // calls the original AsyncCallback passing it the IAsyncResult argument
  return asyncResult => sc.Post(result => callback((IAsyncResult)result), asyncResult);


protected override void OnMouseClick(MouseEventArgs e) 
  // The GUI thread initiates the asynchronous Web request
  Text = "Web request initiated";
  var webRequest = WebRequest.Create("http://Wintellect.com/");
  webRequest.BeginGetResponse(SyncContextCallback(ProcessWebResponse), webRequest);
  base.OnMouseClick(e);


private void ProcessWebResponse(IAsyncResult result) 
  // If we get here, this must be the GUI thread, it's OK to update the UI
  var webRequest = (WebRequest)result.AsyncState;
  using (var webResponse = webRequest.EndGetResponse(result)) 
      Text = "Content length: " + webResponse.ContentLength;
  

这是我在我的应用程序中使用的内容

 public override void UpdateCanvas(object parameter)
 
      Action<Graphpane> startToUpdate = StartToUpdate;
       GraphPane selectedPane = Canvas.HostingPane.PaneList.Find(p =>  p.Title.Text.Equals(defaultPanTitle));
       startToUpdate.BeginInvoke(selectedPane, FormSyncContext.SyncContextCallback(RefreshCanvas), selectedPane);
 

 public static AsyncCallback SyncContextCallback(AsyncCallback callback)
 
       // Capture the calling thread's SynchronizationContext-derived object
       SynchronizationContext sc = SynchronizationContext.Current;

       // If there is no SC, just return what was passed in
       if (sc == null) return callback;

       // Return a delegate that, when invoked, posts to the captured SC a method that
       // calls the original AsyncCallback passing it the IAsyncResult argument
       return asyncResult => sc.Post(result => callback((IAsyncResult)result), asyncResult);
 

【讨论】:

以上是关于将权限/身份验证复制到子线程...?的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中复制具有身份验证的文件

成功身份验证后,Flutter Firebase 数据库权限被拒绝

Spring Boot with Spring Boot:将基本身份验证与JWT令牌身份验证相结合[复制]

gspread 身份验证抛出权限不足

Laravel 5.7 + Spatie 权限 + JWT 身份验证

IIS身份验证和文件操作权限(一身份验证配置)