我想創建一個控制哪些用戶可以查看網站的自定義HttpModule。用於自定義身份驗證的HttpModule如何與Windows身份驗證進行交互?
我想利用Windows身份驗證來做到這一點。
在一個單獨的頁面,我可能會做這樣的事情:
if (HttpContext.Current.User.Identity.Name.Contains("jsmith"))
{
Response.Write("You do not have the correct permissions to view this site.");
Response.End();
}
但是,因爲我想使這個在應用層面更可配置的,我想用一個HttpModule。
這裏是我對代碼所做的開始:
using System;
using System.Web;
public class CustomAuthHttpModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
context.EndRequest += new EventHandler(OnEndRequest);
}
void OnBeginRequest(object sender, EventArgs e) { }
void OnEndRequest(object sender, EventArgs e)
{
HttpApplication appObject = (HttpApplication)sender;
HttpContext contextObject = appObject.Context;
if (contextObject.User.Identity.Name.Contains("jsmith"))
{
contextObject.Response.Clear();
contextObject.Response.End();
}
}
}
我會被罰款使用我的代碼,如果我可以把它放在OnBeginRequest()函數。但是,在運行OnEndRequest()之前,不會在HttpContext對象中創建User屬性。
之前運行代碼將阻止應用程序執行額外的輸出工作,因爲最終會阻止某些用戶被訪問。
有人可以建議一個解決方案 - 這是因爲我的模塊在Windows驗證模塊之前運行,或者是什麼?
...或者,也許有更簡單的方法來做我想用IIS或文件系統權限做什麼?
是不是有一個原因你不讓IIS爲你做這個?或者使用web.config中的``功能? –
CodingGorilla
2010-12-09 17:16:12
你能解釋一下你的意思嗎?我將查找<授權>功能以查看您在那裏談論的內容。 IIS將如何「爲我做這個」? – vwfreak 2010-12-09 18:07:53
`<授權>`!真棒。如果它是一個而不是評論,我會將其標記爲答案。謝謝! – vwfreak 2010-12-09 18:21:30