gpt4 book ai didi

c# - MVC 5 中 PostAuthenticateRequest 管道中的空引用

转载 作者:太空狗 更新时间:2023-10-29 22:26:21 24 4
gpt4 key购买 nike

我正在尝试遵循 Brock Allen 的示例,了解如何使用声明将自定义角色添加到 Windows 角色。( http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/ ) 目前,只需插入他们示例中的代码,我就会收到以下错误。我正在尝试使用仅添加了 Microsoft.IdentityModel 的干净的新 MVC 5 项目。我缺少什么会导致此错误?

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification, Boolean isPostEvent) +22
System.Web.PipelineStepManager.ResumeSteps(Exception error) +1324
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186

这是我的 global.asax.cs 文件中的代码

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
PostAuthenticateRequest += WebApiApplication_PostAuthenticateRequest;
}

void WebApiApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{

var id = ClaimsPrincipal.Current.Identities.First();
id.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender", "potato"));

}
}
}

最佳答案

经过更多挖掘后,似乎是魔法或自动事件包装让这项工作成功了。我不必声明事件处理程序。

namespace WebApplication1
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

void Application_PostAuthenticateRequest()
{
if (Request.IsAuthenticated)
{
var id = ClaimsPrincipal.Current.Identities.First();
}
}
}
}

关于c# - MVC 5 中 PostAuthenticateRequest 管道中的空引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24582256/

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