ASP.NET MVC 5错误重定向登录页面 [英] ASP.NET MVC 5 Wrong Redirect Login Page

查看:184
本文介绍了ASP.NET MVC 5错误重定向登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建ASP.NET MVC 5以个人用户帐户认证模板。
然后,我创建自定义登录页面与窗体身份验证来验证与数据库用户。

I'm creating ASP.NET MVC 5 with "Individual User Accounts" Authentication Template. Then I create custom login page to authenticate user from database with Forms Authentication.

<authentication mode="Forms">
  <forms loginUrl="~/User/SignIn" timeout="2880" protection="All" />
</authentication>

有关测试自定义登录页面,我想补充的HomeController 授权属性。
运行视觉工作室后,重定向到登录页面

For testing the custom login page, I add HomeController with Authorize attribute. After run the visual studio, it redirect to the SignIn page

问题是,当我删除授权属性在的HomeController 和FilterConfig.cs

The problem is when I remove Authorize attribute at HomeController and add authorize filter at FilterConfig.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new AuthorizeAttribute());
}

我有HTTP错误:HTTP错误401.0 - 未经授权。如何解决这一问题?

I've got HTTP error: "HTTP Error 401.0 - Unauthorized". How to fix this?

推荐答案

由于我把正确答案为注释至少8分钟前,任何人都没有,我会添加它作为一个答案。

Because i put the correct answer as a comment at least 8 mins before anyone did I'll add it as an answer.

您基本上需要到[使用AllowAnonymous]属性添加到您的登入动作,因为基本上你所创建的过滤器是使每一个动作需要授权来观看。

You basically need to add the [AllowAnonymous] attribute to your SignIn action, because essentially the filter you have created is making every action require authorisation to 'view'.

从<一个拍摄href=\"http://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous\"相对=nofollow>使用AllowAnonymous属性信息

......在ASP.NET MVC 4,它在烤烤由在,我的意思是:有一个内置AllowAnonymousAttribute中的System.Web.Mvc命名空间而白名单的匿名访问的行为。

"...in ASP.NET MVC 4, it's baked in. By baked in, I mean that: There's a built-in AllowAnonymousAttribute in the the System.Web.Mvc namespace which whitelists actions for anonymous access."

从<一个拍摄href=\"http://odeto$c$c.com/blogs/scott/archive/2011/01/18/configurable-global-action-filters-for-asp-net-mvc.aspx\"相对=nofollow>全球行动筛选信息

ASP.NET MVC 3.0中引入的全球行动过滤器 - 一个简单的方法,以一个动作过滤器适用于MVC应用程序中的每一个动作

"ASP.NET MVC 3.0 introduces global action filters - an easy way to apply an action filter to every action in an MVC application."

[AllowAnonymous]
public ActionResult SignIn() {
}

这篇关于ASP.NET MVC 5错误重定向登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆