本文介绍了授权属性在MVC 4个工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用我使用
System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=2.0.0.0
作为我的会员供应商,在这里我的账户控制器code
as my membership provider and here my Account Controller code
[HttpPost]
public ActionResult Login(string username,string password)
{
var result = Membership.ValidateUser(username, password);
if(result)
{
var user = Membership.GetUser(username);
var roles = Roles.GetRolesForUser(username);
var isDistributor = roles.Any(x => x.ToUpper() == "DISTRIBUTOR");
if (isDistributor) return RedirectToAction("ShowCurrentDistributor", "Distributor");
}
else
{
TempData["error"] = "Invalid login attempt";
}
return View();
}
和我ShowCurrentDistributor行动code:
And my ShowCurrentDistributor action code:
[HttpGet]
[Authorize(Roles = "Distributor")]
public ActionResult ShowCurrentDistributor()
{
var distributor = _distributer.GetDistributerbyEmail(User.Identity.Name);
return View(distributor);
}
但是,当我打电话的 ShowCurrentDistributor 方式,授权不工作。它总是返回到我的登录表单,即使我用有效的角色(分销商)认证通过。什么是错我的code
But when i call ShowCurrentDistributor method, authorization not working. it always return back to my login form even i passed authentication with valid role( Distributor). Whats wrong with my code
推荐答案
您需要验证成功后添加以下行:
You need to add the following line after successful validation:
FormsAuthentication.SetAuthCookie(username,true);
(由原始的海报有问题的评论回答)
(answered in question comments by the original poster)
这篇关于授权属性在MVC 4个工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!