Ich habe ein grundlegendes MVC 4-Projekt erstellt. Der HomeController und Home \ Index.cshtml und ContactUs.cshtml wurden hinzugefügt. Hinzufügen einer Route in Global.asax für ContactUs.MVC 4 überschreiben AuthorizeAttribute funktioniert nicht
Fügen Sie eine Ordnerauth hinzu und fügen Sie eine Klasse Auth.css in Auth-Ordner hinzu.
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;
}
}
}
In Homecontroller
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
//
// GET: /Home/
[Auth]
public ActionResult ContactUs()
{
return View();
}
}
Das Problem ist, wenn Sie den Code und unter http auszuführen: // localhost: [Portnummer hier]/Home/ContactUs, es nicht auf die Überschreibung Klasse getroffen hat AuthAttribut.
Hat der Code etwas falsch?
, was Sie erreichen wollen? Es ist nicht offensichtlich ... –
Ich versuche, die [Authorize] zu [Auth] anzupassen und auf Cotroller zu verwenden. Dies gibt mir die Flexibilität, meine eigene Authentifizierung zu erstellen, anstatt die Standardmitgliedschaft zu verwenden. – user2130167
Das versuche ich zu erreichen. Einziger Unterschied ist es MVC 4 aber nicht für MVC3. http://weblogs.asp.net/jgalloway/archive/2012/05/04/asp-net-mvc-authentication-customizing-authentication-and-authorization-the-right-way.aspx – user2130167