ASP.NET MVC 4用户认证 [英] ASP.NET MVC 4 User Authentication
问题描述
我试图写验证和授权用户与ASP.NET MVC 4现在的问题是开发了我的网站上登录的方法,尽管我称之为FormsAuthentication.SetAuthCookie方法验证用户登录方法内后重定向以自拍动作,User.Identity.IsAuthenticated返回我的自定义还是假的授权属性对象。
I am trying to write a Login method that authenticates and authorizes users into my web site developed with ASP.NET MVC 4. The problem is, although I call the FormsAuthentication.SetAuthCookie method after validating the user inside the Login method and redirect to ViewProfile action, User.Identity.IsAuthenticated returns still false in my custom Authorize attribute object.
我给下面的code:
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel model)
{
if (Membership.ValidateUser(model.Username, model.Password))
{
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
return this.RedirectToAction("ViewProfile");
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
[MyAuthorize(Roles = "Super Role, Seeker")]
public ActionResult ViewProfile()
{
if (ModelState.IsValid)
{
...
}
}
这里是MyAuthorizeAttribute类的code
And here is the code of the MyAuthorizeAttribute class
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
var user = httpContext.User;
if (!user.Identity.IsAuthenticated)
return false;
...
}
}
我是什么失踪?
推荐答案
有关谁可能使用FormsAuthentication,面对类似情况,要确保在你的web.config文件中存在以下配置的那些
For the ones who may use FormsAuthentication and face a similar situation, be sure that the following configuration exists under your web.config file
<authentication mode="Forms"></authentication>
现在一切正常。
这篇关于ASP.NET MVC 4用户认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!