私は基本的なMVC 4プロジェクトを作成しました。 HomeControllerとHome \ Index.cshtmlとContactUs.cshtmlを追加しました。 連絡先にGlobal.asaxのルートを追加します。MVC 4 Override AuthorizeAttribute not working
フォルダの認証を追加し、AuthフォルダにAuth.cssクラスを追加します。 HomeController
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
//
// GET: /Home/
[Auth]
public ActionResult ContactUs()
{
return View();
}
}
で
using System;
using System.Web;
using System.Web.Http;
using System.Net.Http;
namespace MvcApplicationTestProject1
{
public class AuthAttribute : AuthorizeAttribute
{
//public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
//{
// HandleUnauthorizedRequest(actionContext);
//}
protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
{
var response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.Redirect);
response.Headers.Add("Location", "http://www.google.com");
actionContext.Response = response;
}
//MVC 4 Web.Http.AuthorizeAttribute has IsAuthorized function but not AuthorizeCore
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
{
return false;
}
}
}
コードを実行して、httpを訪問したときに問題がある:// localhostを:[ポート番号ここ] /ホーム/お問い合わせ、 それはオーバーライドクラスにヒットしません。 AuthAttribute。
コードに問題がありますか?
をあなたが達成しようとしていますか?その明らかではない... –
[認証]を[認証]にカスタマイズし、それをcotrollersで使用しようとしています。これにより、標準のメンバーシップを使用する代わりに自分の認証を構築する柔軟性が得られます。 – user2130167
これは私が達成しようとしているものです。違いはMVC 4だけですが、MVC 3ではありません。 http://weblogs.asp.net/jgalloway/archive/2012/05/04/asp-net-mvc-authentication-customizing-authentication-and-authorization-the-right-way.aspx – user2130167