Pra*_*nav 5 session c#-4.0 asp.net-mvc-4
我有一个网站,如果他们没有登录,我希望用户被重定向到"登录"页面.用户可以尝试通过发布网址来访问网页.我想在C#MVC 4.5中做到这一点
除非已登录,否则我不希望"[授权]"操作可用.它是查看索引页面的索引操作.
//Login Controller
[AllowAnonymous]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection frm)
{
....//other code
}
[Authorize]
public ActionResult Index()
{
List<DTO> obj = objConfigurator.GetDataList();
return View(obj);
}
public ActionResult Edit(int Id)
{
DTO obj = objC.GetListById(Id);
return View(obj);
}
Run Code Online (Sandbox Code Playgroud)
vor*_*tex 14
使用[Authorize]控制器上的属性.
[Authorize]
public class YourController: Controller
{
. . .
[AllowAnonymous]
public ActionResult Register()
{
}
[AllowAnonymous]
public ActionResult LogIn()
{
}
. . .
}
Run Code Online (Sandbox Code Playgroud)
此外,您必须在web.config中添加您的登录页面 -
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login" timeout="2880" />
</authentication>
</system.web>
Run Code Online (Sandbox Code Playgroud)
您还有另一个更好的选择,即AuthorizeAttribute 在global.asax文件中注册为全局过滤器.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
....
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
}
Run Code Online (Sandbox Code Playgroud)
这样,您只需要应用[AllowAnonymous]您希望被无用用户访问的操作.
| 归档时间: |
|
| 查看次数: |
11385 次 |
| 最近记录: |