Mic*_*ael 5 c# asp.net asp.net-mvc claims-based-identity asp.net-mvc-5
我试图按照Brock Allen的例子来说明如何使用声明向Windows角色添加自定义角色.(http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in -asp-net-using-claims /)目前,只是从他们的示例中插入代码我得到以下错误.我正在尝试使用仅添加了Microsoft.IdentityModel的干净的新MVC 5项目.我错过了什么会导致这个错误?
你调用的对象是空的.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例.
来源错误:
在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.
堆栈跟踪:
[NullReferenceException:对象引用未设置为对象的实例.]
System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification,Boolean isPostEvent)+22
System.Web.PipelineStepManager.ResumeSteps(异常错误)+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"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
经过更多的挖掘,它似乎是魔术或自动事件包装,使这项工作.我不必声明一个事件处理程序.
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)