ASP.Net MVC 4一般主要困难 [英] ASP.Net MVC 4 Generic Principal Difficulties

查看:106
本文介绍了ASP.Net MVC 4一般主要困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发的 ASP.Net MVC 4 Web应用程序。 previously我的MVC应用程序都使用的 MVC 3 并与这个新的 MVC 4 应用程序,我刚才复制/重用我的验证和授权$ C $开发从previous应用ç

I am developing an ASP.Net MVC 4 web application. Previously my MVC applications have been developed using MVC 3 and with this new MVC 4 application I have just copied/ reused my authentication and authorisation code from previous applications.

当用户登录到我的地盘我做以下

When a user logs into my site I do the following

帐户控制器

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

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

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;

}

这code效果很好在我的previous MVC 3应用程序,但在这个MVC 4应用程序,剃刀视图里面,下面的code似乎并没有被访问的 IsInRole 属性进行检查的作用

This code works well in my previous MVC 3 applications, but in this MVC 4 application, inside the Razor View, the following code does not seem to be accessing the IsInRole property to perform the role check

@if (HttpContext.Current.User.IsInRole("Applicant"))
{
    <p>text</text>
}

同样,这个工作完全在我的MVC 3应用程序。

Again, this worked perfectly in my MVC 3 applications.

有没有人有任何意见或建议,为什么这会不会与我的MVC 4应用工作的?

Does anyone have any ideas or suggestions as to why this won't work with my MVC 4 application?

任何帮助很多AP preciated。

Any help is much appreciated.

感谢。

额外的资讯

我的MVC 4应用程序是使用.NET Framework 4.0

My MVC 4 application is using .Net Framework 4.0

下面的截图显示了我的通用首席这是分配给 Context.User 。你可以看到这个用户,在 m_roles 包含两个字符串,在用户名(100170)和他们的角色(申请人)。但由于某些原因,在 IsInRoles 不能访问或在我的看到MVC 4 的Razor视图,但是,现在已经在我相同的 MVC 3 剃须刀视图。

The screenshot below shows my Generic Principal which is assigned to Context.User. You can see that for this User, the m_roles contains two strings, the UserID (100170) and their Role(Applicant). But for some reason, The IsInRoles cannot be accessed or seen in my MVC 4 Razor View, however, it could in my identical MVC 3 Razor View.

推荐答案

乡亲

我终于解决了这个问题。它默认显示在 SimpleMembershipProvider 当你创建一个新的ASP.NET MVC应用程序4启用。我不想使用 SimpleMembershipProvider 在此之际,但是,我需要在我的web配置与以下行来禁止它。

I finally resolved this issue. It appears by default the SimpleMembershipProvider is enabled when you create a new ASP.NET MVC 4 application. I did not want to use the SimpleMembershipProvider on this occasion, however, I needed to disable it in my web config with the following line

<appSettings>
    <add key="enableSimpleMembership" value="false" />
</appSettings>

我要调用的 User.IsInRole 现在的伟大工程。

希望这可以帮助其他人。

Hope this helps someone else.

这篇关于ASP.Net MVC 4一般主要困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆