gpt4 book ai didi

c# - 如何在 asp.net 中设置自定义主体

转载 作者:行者123 更新时间:2023-11-30 22:20:18 25 4
gpt4 key购买 nike

我是网络应用程序的新手。我创建了一个自定义原则并尝试在 CurrentDomain 中设置它。此代码在 WPF 应用程序中完美运行。但这里抛出策略异常“默认主体对象不能设置两次。”

 var principal = new CustomPrinciple(currentIdentity);
Thread.CurrentPrincipal = principal;
AppDomain.CurrentDomain.SetThreadPrincipal(Thread.CurrentPrincipal);

我的 CustomPrincple 派生自 ClaimsPrinciple

  public class CustomPrinciple : ClaimsPrincipal
{ }

我想知道为什么不允许我在这里设置它。如何在 Web 应用程序中设置自定义原则。

最佳答案

自定义主体应该在 global.asax 文件中为发出的请求设置,而不是线程。然后你从 HttpContext.User Property 中获取自定义主体

这个问题的答案详细说明了最好的方法:ASP.NET MVC - Set custom IIdentity or IPrincipal

关于c# - 如何在 asp.net 中设置自定义主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15062608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com