我有一個ASP.NET MVC網站,我想添加一個小型管理頁面。我遇到的問題是,我將全面部署這一部分,並且我不會提供SSL。我確定要求管理員遠程桌面並使用本地瀏覽器執行管理。將ASP.NET MVC App管理站點鎖定到LocalHost
可以這樣做嗎?除了我的管理頁面之外,我基本上希望獲得與<customeErrors mode="RemoteOnly" />
相同的行爲。我可以通過web.config做到這一點如何?
我有一個ASP.NET MVC網站,我想添加一個小型管理頁面。我遇到的問題是,我將全面部署這一部分,並且我不會提供SSL。我確定要求管理員遠程桌面並使用本地瀏覽器執行管理。將ASP.NET MVC App管理站點鎖定到LocalHost
可以這樣做嗎?除了我的管理頁面之外,我基本上希望獲得與<customeErrors mode="RemoteOnly" />
相同的行爲。我可以通過web.config做到這一點如何?
Request.IsLocal
是你的朋友。
http://msdn.microsoft.com/en-us/library/system.web.httprequest.islocal.aspx
你可以用它來檢查的請求從本地機器的到來。
自定義屬性
然後,您可以擴大這是一個自定義屬性,但可能是矯枉過正。如果是這樣的路線選擇,這是一個很好的例子,做類似的事情:
Custom Attributes on ActionResult
MVC3起,您可以在控制器級別設置的屬性,而不是方法,所以你可以鎖定訪問整個控制器負責管理頁面。
我做到了,寫一個客戶屬性,像這樣:
public class IsLocalAttribute : AuthorizeAttribute
{
public bool ThrowSecurityException { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isLocal = httpContext.Request.IsLocal;
if (!isLocal && ThrowSecurityException)
throw new SecurityException();
return isLocal;
}
}
整個控制器上的基本用法:
[IsLocal]
public class LocalOnlyController : Controller
{
public ActionResult Index()
{
return View();
}
}
或特定方法:
public class SomeController : Controller
{
[IsLocal]
public ActionResult LocalOnlyMethod()
{
return View();
}
}
如果您想拋出安全異常而不是302重定向:
public class SomeController : Controller
{
[IsLocal(ThrowSecurityException = true)]
public ActionResult LocalOnlyMethod()
{
return View();
}
}
將我自己的Authorize屬性在其核心使用'Request.IsLocal'屬性是否合理? – Nate 2013-02-13 16:59:48
當然。請參閱我編輯中的鏈接。 – KingCronus 2013-02-13 17:01:23