在 ASP.Net 中,我们之前写过这样的授权代码:
public class PageBase : System.Web.UI.Page
{
protected override void OnInit(System.EventArgs e)
{
string CurrentPath = HttpContext.Current.Request.Url.AbsolutePath.ToLower();
//Check If User Access to this Path
if(WebUser.Access(CurrentPath) == false)
{
Reposponse.Redirect("Loagin.aspx");
}
}
}
并且WebUser
是包含用户数据的会话:
public User WebUser
{
get
{
if (HttpContext.Current.Session["User"] != null)
{
return (User)HttpContext.Current.Session["User"];
}
else
{
HttpContext.Current.Response.Redirect("Login.aspx", true);
return null;
}
}
}
我们从PageBase
.
现在,如果我想使用MVC
可以编写在每个请求上运行的代码的地方编写类似的代码?
感谢
您想根据您的问题在 MVC 中对用户进行身份验证和授权。您可以使用身份验证和授权过滤器对用户进行身份验证和授权。这很简单,
请查看链接以检查我们如何创建身份验证和授权过滤器以及我们如何在代码中使用它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句