我正在尝试按照Brock Allen的示例,使用声明添加自定义角色到Windows角色中。(http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/)。但是,我只是从他们的示例中复制粘贴代码,就出现了以下错误。我正在使用仅添加了Microsoft.IdentityModel的全新MVC 5项目进行尝试。我漏掉了什么导致了这个错误?
这是我global.asax.cs文件中的代码。对象引用未设置为对象的实例。说明: 当前Web请求执行期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误的更多信息以及错误在代码中的起源和位置。
异常详细信息:System.NullReferenceException: 对象引用未设置为对象的实例。
源错误:
当前Web请求的执行期间生成了一个未处理的异常。可以使用下面的异常堆栈跟踪标识异常的来源和位置。
堆栈跟踪:
[NullReferenceException: 对象引用未设置为对象的实例。]
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
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"));
}
}
}
Application_Start
方法中,尚未加载完所有的事件处理程序模块。 在事件处理程序模块加载完成后,ASP.NET会使用“_EventHandlerMethod”的约定来正确连接你的方法。 如果你想给这些方法起其他名字,可以在Init()
方法中这样做。 - MisterHux