asp.net-mvc – ASP.Net MVC 4通用主要困难
|
我正在开发一个ASP.Net MVC 4 Web应用程序。以前,我的MVC应用程序已经使用MVC 3开发,并且使用这个新的MVC 4应用程序,我刚从以前的应用程序复制/重用了我的认证和授权码。 当用户登录到我的网站时,我会执行以下操作 帐号控制器 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;
}
此代码在我以前的MVC 3应用程序中运行良好,但在MVC 4应用程序中,在Razor View中,以下代码似乎没有访问IsInRole属性来执行角色检查 @if (HttpContext.Current.User.IsInRole("Applicant"))
{
<p>text</text>
}
再次,这在我的MVC 3应用程序中完美地工作。 有没有人有任何想法或建议,为什么这不会与我的MVC 4应用程序? 任何帮助深表感谢。 谢谢。 额外信息 我的MVC 4应用程序正在使用.Net Framework 4.0 下面的截图显示了我的General Principal,它被分配给Context.User。您可以看到,对于该用户,m_roles包含两个字符串,即UserID(100170)及其角色(申请人)。但是由于某种原因,IsInRoles不能在我的MVC 4 Razor View中访问或看到,但是它可以在我的MVC 3 Razor View中。 解决方法乡亲我终于解决了这个问题。默认情况下,您创建新的ASP.NET MVC 4应用程序时,将启用SimpleMembershipProvider。我不想在这个场合使用SimpleMembershipProvider,但是,我需要在我的web配置中禁用它,具有以下一行 <appSettings>
<add key="enableSimpleMembership" value="false" />
</appSettings>
我给User.IsInRole的电话工作很棒。 希望这有助于别人。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 是否可以在将razor viewengine发送到客户
- asp经典 – ASP Classic中的注释代码
- asp.net – 当HTTPContext .Current为Nothing时如何使用Ser
- asp.net-mvc – 如何使用ASP.NET MVC 5和OWIN获取Facebook的
- asp.net-mvc – 如何将可视化设计器集成到开发过程中?
- asp.net-mvc – ASP.NET MVC – 服务层 – 业务层 – 数据层
- asp.net – 形式auth超时和会话超时的差异
- asp.net-mvc – EditorTemplates / Object.cshtml使用Edito
- 转:[WebServices]介绍
- asp.net-mvc – ASP.NET MVC和Visual Studio 2013:编译错误
- asp.net-mvc – MVC Preview 5 – 将视图呈现为字
- asp.net-mvc – 如何在升级到ASP.NET MVC 5和Web
- 防止双击asp.net按钮
- asp.net – Web API 2 – ApiController.Interna
- asp.net – IIS 8.0中内核模式和用户模式缓存之间
- asp.net-mvc – 在C#中,我如何从一个字节[]中知道
- asp.net – IIS在编译的.net站点中查找.cs文件
- asp.net-mvc – 使用Castle Windsor在ASP.NET MV
- IIS 7/ASP.Net管道如何工作?
- asp.net-mvc-3 – ASP.NET成员资格 – 本地登录,
