首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.Net MVC4通用主要困难

ASP.Net MVC4通用主要困难
EN

Stack Overflow用户
提问于 2013-02-08 19:57:06
回答 3查看 8K关注 0票数 17

我正在开发一个ASP.Net MVC4 web应用程序。以前,我的MVC应用程序是使用MVC 3开发的,使用这个新的MVC 4应用程序,我刚刚从以前的应用程序复制/重用了我的身份验证和授权码

当用户登录到我的站点时,我执行以下操作

客户控制器

代码语言:javascript
复制
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        User user = _userService.GetUser(model.Email.Trim());

        //Create Pipe Delimited string to store UserID and Role(s)
        var userData = user.ApplicantID.ToString();

        foreach (var role in user.UserRoles)
        {
            userData = userData + "|" + role.description;
        }

        _formAuthService.SignIn(user.ApplicantFName, false, userData);

        return RedirectToAction("Index", "Portfolio");
        }

        return View(model);
    }

FormsAuthenticationService

代码语言:javascript
复制
public class FormsAuthenticationService : IFormsAuthenticationService
{
    public void SignIn(string userName, bool createPersistentCookie, string UserData)
    {
        if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

        // Create and tuck away the cookie
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(15), createPersistentCookie, UserData);
        // Encrypt the ticket.
        string encTicket = FormsAuthentication.Encrypt(authTicket);

        //// Create the cookie.
        HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
        HttpContext.Current.Response.Cookies.Add(faCookie);
    }
}

Global.asax

代码语言:javascript
复制
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{

    // Get the authentication cookie
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    // If the cookie can't be found, don't issue the ticket
    if (authCookie == null) return;

    // Get the authentication ticket and rebuild the principal
    // & identity
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    string[] UserData = authTicket.UserData.Split(new Char[] { '|' });

    GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
    GenericPrincipal userPrincipal = new GenericPrincipal(userIdentity, UserData);
    Context.User = userPrincipal;

}

这段代码在我以前的MVC3应用程序中工作得很好,但是在这个MVC4应用程序中,在Razor视图中,以下代码似乎没有访问IsInRole属性来执行角色检查

代码语言:javascript
复制
@if (HttpContext.Current.User.IsInRole("Applicant"))
{
    <p>text</text>
}

同样,这在我的MVC3应用程序中工作得很好。

有没有人有任何想法或建议来解释为什么这不适用于我的MVC4应用程序?

任何帮助都是非常感谢的。

谢谢。

额外信息

我的MVC4应用程序使用的是.Net Framework4.0

下面的屏幕截图显示了分配给Context.User.的通用主体您可以看到,对于此用户,m_roles包含两个字符串:UserID (100170)及其Role(Applicant).但是由于某些原因,IsInRoles无法在我的MVC 4剃刀视图中访问或查看,但是,它可以在我相同的MVC 3剃刀视图中访问或查看。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14772034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档