小编Vik*_*tor的帖子

如何在ASP.NET MVC中更轻松地访问我的自定义IPrincipal?

我编写了一个自定义主体对象,其中包含一些额外的字段(除了用户名之外的电子邮件和用户ID).

为了访问这些属性,我必须将Context.User对象转换为我的自定义主体.

@Html.GetGravitarImage((User as CustomPrincipal).Email)
Run Code Online (Sandbox Code Playgroud)

通过我的global.ascx中的Application_AuthenticateRequest创建/反序列化此自定义主体.您可以在此处查看此问题以获取更多信息.

private void Application_AuthenticateRequest(Object source, EventArgs e)
{
    var application = (HttpApplication)source;
    var context = application.Context;

    // Get the authentication cookie
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = context.Request.Cookies[cookieName];
    if (authCookie == null)
        return;

    var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    context.User = CustomPrincipal.CreatePrincipalFromCookieData(authTicket.UserData);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果用户未经过身份验证,那么我对CustomPrincipal的强制转换将失败(因为它不会在上面的方法中注入),并且(User as CustomPrincipal)的结果将返回null,从而给我一个空引用我上面的方法尝试获取电子邮件时的异常.

什么是这个问题的干净解决方案?我想让访问我的自定义主体变得容易,并且必须执行以下操作似乎很麻烦:

@Html.GetGravitarIcon((User is CustomPrincipal) ? (User as CustomPrincipal).Email : "Default Email")
Run Code Online (Sandbox Code Playgroud)

这是处理这种情况的唯一方法吗?

c# asp.net-mvc casting iprincipal razor

9
推荐指数
1
解决办法
9728
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

casting ×1

iprincipal ×1

razor ×1