我正在構建一個內聯網應用程序使用MVC3與MSSQL後端。我有身份驗證和角色(通過自定義角色提供者)正常工作。我現在要做的是重寫User.Identity以允許像User.Identity.FirstName這樣的項目。但我無法找到任何代碼,會告訴我如何的WindowsIdentityMVC3 Windows身份驗證覆蓋User.Identity
爲此,我曾嘗試編寫自定義提供:
public class CPrincipal : WindowsPrincipal
{
UserDAL userDAL = new UserDAL();
public CPrincipal(WindowsIdentity identity)
: base(identity)
{
userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
this.identity = identity;
}
public UserInfo userInfo { get; private set; }
public WindowsIdentity identity { get; private set; }
}
並重寫WindowsAuthentication來填充自定義主體。
void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
if (e.Identity != null && e.Identity.IsAuthenticated)
{
CPrincipal cPrincipal = new CPrincipal(e.Identity);
HttpContext.Current.User = cPrincipal;
}
}
我在認證功能中有一個斷點,並且正在填充主體;然而,當我在控制器中放置一個斷點時,用戶只是它的普通RolePrincipal,而不是我的自定義主體。我究竟做錯了什麼?
編輯:
我註釋掉上面的代碼在Global.asax。 我已經覆蓋了AuthorizeAttribute使用C#:
public class CAuthorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool authorized = base.AuthorizeCore(httpContext);
if (!authorized)
{
return false;
}
IIdentity user = httpContext.User.Identity;
CPrincipal cPrincipal = new CPrincipal(user);
httpContext.User = cPrincipal;
return true;
}
}
和調整,我主要在以下幾點:
public class CPrincipal : IPrincipal
{
private UserDAL userDAL = new UserDAL();
public CPrincipal(IIdentity identity)
{
userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
this.Identity = identity;
}
public UserInfo userInfo { get; private set; }
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
}
現在,我當我把一個斷點,手錶顯示在用戶執行以下操作:
- 用戶
- [CSupport.Model.CPrincipal]
- 身份
身份是accessable;但是,它仍然是WindowsIdentity CPrincipal只能在手錶中訪問,不能直接訪問。
編輯: 感謝所有對此有貢獻的人。您已經極大地擴展了我對各部分工作原理的理解。
我有兩種工作方式,所以我想我會分享。
選項1:覆蓋的授權請求在Global.asax中
這是一個我用下去。
我沒有使用Application_AuthenticateRequest,因爲(根據這個:HttpContext.Current.User is null even though Windows Authentication is on)用戶沒有在Windows身份驗證過程中填充,因此沒有任何可以用來獲取用戶信息的東西。
Application_AuthorizeRequest是鏈中的下一個,並在窗口標識被引入後發生。
protected void Application_AuthorizeRequest(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated && Roles.Enabled)
{
Context.User = new FBPrincipal(HttpContext.Current.User.Identity);
}
}
這是主要的
public class CPrincipal : IPrincipal
{
private UserDAL userDAL = new UserDAL();
public CPrincipal(IIdentity identity)
{
userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
this.Identity = identity;
}
public UserInfo userInfo { get; private set; }
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
return userDAL.IsUserInRole(userInfo.UserName, role);
}
}
的覆蓋這是你如何訪問所創建的新的主要更新的信息。
[Authorize(Roles = "super admin")]
public ActionResult Dashboard()
{
string firstname = (User as CPrincipal).userInfo.FirstName; // <--
DashboardModel dModel = reportDAL.GetChartData();
return View(dModel);
}
選項2:重寫AuthorizeAttribute
這是被覆蓋的主(這是與上述相同)
public class CPrincipal : IPrincipal
{
private UserDAL userDAL = new UserDAL();
public CPrincipal(IIdentity identity)
{
userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
this.Identity = identity;
}
public UserInfo userInfo { get; private set; }
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
return userDAL.IsUserInRole(userInfo.UserName, role);
}
}
這裏是授權的重寫屬性
public class CAuthorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool authorized = base.AuthorizeCore(httpContext);
if (!authorized)
{
return false;
}
IIdentity user = httpContext.User.Identity;
CPrincipal cPrincipal = new CPrincipal(user);
httpContext.User = cPrincipal;
return true;
}
}
這是您更改哪個AuthorizeAttribute使用和使用新信息的地方灰。
[CAuthorize(Roles = "super admin")] // <--
public ActionResult Dashboard()
{
string firstname = (User as CPrincipal).userInfo.FirstName; // <--
DashboardModel dModel = reportDAL.GetChartData();
return View(dModel);
}
選項1全局處理everthing,選項2處理個人級別的所有內容。
等等...什麼是事件處理程序?你不是在嘗試使用ASP.NET登錄控件嗎?這個活動位於哪裏,哪個活動與哪個活動相搭配? –
我在內部網站上使用Windows身份驗證。此事件處理程序位於全局中。asax –
global.asax中沒有OnAuthenticate處理程序。這可能是你遇到問題的原因。 –