ASP.net MVC中的自定义表单身份验证/授权方案
发布时间:2020-05-23 19:33:51 所属栏目:asp.Net 来源:互联网
导读:我正在尝试使用表单身份验证在ASP.NET MVC中创建自定义身份验证方案.我可能在网站上有不同区域的想法 – 审批者是和一般用户区域,这些将使用不同的登录页面,等等.所以这就是我想要发生的事情. 用户访问受限页面(现在我用客户AuthorizeAttribute保护它) 用户被
|
我正在尝试使用表单身份验证在ASP.NET MVC中创建自定义身份验证方案.我可能在网站上有不同区域的想法 – 审批者是和一般用户区域,这些将使用不同的登录页面,等等.所以这就是我想要发生的事情. >用户访问受限页面(现在我用客户AuthorizeAttribute保护它) 非常感谢任何帮助! 这就是我到目前为止所做的,它不起作用: public class AdministratorAccountController : Controller
{
public ActionResult Login()
{
return View("Login");
}
[HttpPost]
public ActionResult Login(AdministratorAccountModels.LoginModel model,string returnUrl)
{
if (ModelState.IsValid)
if (model.UserName == "admin" && model.Password == "pass") // This will be pulled from DB etc
{
var ticket = new FormsAuthenticationTicket(1,// version
model.UserName,// user name
DateTime.Now,// create time
DateTime.Now.AddSeconds(30),// expire time
false,// persistent
""); // user data
var strEncryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,strEncryptedTicket);
Response.Cookies.Add(cookie);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index","Home");
}
}
else
{
ModelState.AddModelError("","The user name or password provided is incorrect.");
}
// If we got this far,something failed,redisplay form
return View(model);
}
[AdministratorAuthorize]
public ActionResult MainMenu()
{
return View();
}
public class AdministratorAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authenCookie = httpContext.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
if (authenCookie == null) return false;
var ticket = FormsAuthentication.Decrypt(authenCookie.Value);
var id = new FormsIdentity(ticket);
var astrRoles = ticket.UserData.Split(new[] { ',' });
var principal = new GenericPrincipal(id,astrRoles);
httpContext.User = principal;
return true;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var model = new AdministratorAccountModels.LoginModel();
var viewData = new ViewDataDictionary(model);
filterContext.Result = new ViewResult { ViewName = "Login",ViewData = viewData };
}
}
}
解决方法我使用了减号和上面我自己的代码建议的代码组合来创建这个可能对其他人有帮助的简化方案.我添加了一些关于最初让我困惑的事情的评论.public class AdministratorAccountController : Controller
{
public ActionResult Login()
{
return View("Login");
}
[HttpPost]
public ActionResult Login(AdministratorAccountModels.LoginModel model,string returnUrl)
{
if (ModelState.IsValid)
// Here you would call a service to process your authentication
if (model.UserName == "admin" && model.Password == "pass")
{
// * !!! *
// Creating a FromsAuthenticationTicket is what
// will set RequestContext.HttpContext.Request.IsAuthenticated to True
// in the AdminAuthorize attribute code below
// * !!! *
var ticket = new FormsAuthenticationTicket(1,// persistent
""); // user data,such as roles
var strEncryptedTicket = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,strEncryptedTicket);
Response.Cookies.Add(cookie);
// Redirect back to the page you were trying to access
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index",redisplay form
return View(model);
}
[AdminAuthorize]
public ActionResult MainMenu()
{
return View();
}
public class AdminAuthorize : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
{
// Redirect to the needed login page
// This can be pulled from config file or anything else
filterContext.HttpContext.Response.Redirect("/AdministratorAccount/Login?ReturnUrl="
+ HttpUtility.UrlEncode(filterContext.HttpContext.Request.RawUrl));
}
base.OnActionExecuting(filterContext);
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 为什么需要为Html.Action定义的路由?
- asp.net-mvc – ASP.MVC 2.0当整数值为零时,如何显示整数的
- iis-6 – IIS 6拒绝访问默认文档
- asp.net文件下载 – 跟踪下载的大小
- asp.net-mvc – ASP.NET MVC是否有任何DateTime路由约束?
- asp.net-mvc – 如何将asp.net mvc集成到Web站点项目中
- asp.net mvc添加到AUTHORIZE属性
- asp.net-mvc – 将Castle Windsor与SignalR集成 – 我该如何
- asp.net-mvc – ASP.NET MVC是否有分页解决方案,在数据库中
- asp.net-mvc-3 – 带区域的MVC – Html.ActionLink返回错误
推荐文章
站长推荐
- asp.net – web.config中的多个/不同的身份验证设
- asp.net-mvc-3 – mvc3在另一个内部嵌入了局部视
- asp.net-mvc – asp.net mvc 4应用程序的入口点是
- asp.net – 插入后在实体框架中获取记录ID
- asp.net-mvc-3 – 将字符串数组绑定到MVC Razor中
- 为什么默认的ASP.NET Forms认证Cookie在其默认名
- asp.net – 获取数据绑定到ListView上DataBound事
- asp.net – 没有回发的日历控件
- asp.net – 如何使Owin自主主机支持Json输出?
- ASP.NET Web App和ASP.NET MVC 3 Empty Web App之
热点阅读
