好像我最初的做法是行不通的。我試圖讓ASP.NET從他們的AD帳戶中自動加載用戶角色。沒有評論這是否可能。然而,我所做的研究表明,我必須編寫代碼將AD組成員加載到用戶角色中。
創建ASP.NET MVC使用的用戶主體的解決方案似乎是在FormsAuthentication_OnAuthenticate()中創建它並將其分配給Context.User。看起來如果我沒有設置Context.User ASP.NET MVC根據FormsAuthentication_OnAuthenticate()返回後的驗證票據創建一個用戶主體。另外,如果我將它設置在FormsAuthentication_OnAuthenticate()中,ASP.NET MVC似乎對Context.User不起作用。
以下是我最終做的。
這是處理認證
public ActionResult LogOn(FormCollection collection, string returnUrl)
{
// Code that authenticates user against active directory
if (authenticated)
{
var authTicket = new FormsAuthenticationTicket(username, true, 20);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authCookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(authCookie);
if (Url.IsLocalUrl(returnUrl)
&& returnUrl.Length > 1
&& returnUrl.StartsWith("/", StringComparison.OrdinalIgnoreCase)
&& !returnUrl.StartsWith("//", StringComparison.OrdinalIgnoreCase)
&& !returnUrl.StartsWith("/\\", StringComparison.OrdinalIgnoreCase))
{
return Redirect(returnUrl);
}
else
{
return Redirect("~/");
}
}
return View();
}
我最初嘗試只是打電話FormsAuthentication.SetAuthCookie(用戶名,真實),而不是手動創建,加密,並將其添加到響應的cookie集合的代碼。這在開發環境中起作用。但是,這並沒有在我發佈到網站後。
這是註銷代碼
public ActionResult LogOff()
{
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
authCookie.Expires = DateTime.Today.AddDays(-1);
}
Response.Cookies.Add(authCookie);
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
FormsAuthentication.SignOut()似乎並沒有做任何事情,我切換到手動創建,加密,並在加入身份驗證票響應Cookie集合後登錄代碼。所以我必須手動過期cookie。
這是我的代碼有FormsAuthentication_OnAuthenticate()
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs args)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || string.IsNullOrWhiteSpace(authCookie.Value))
return;
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
UserData userData = null;
if (Application["UserData_" + authTicket.Name] == null)
{
userData = new UserData(authTicket.Name);
Application["UserData_" + authTicket.Name] = userData;
}
else
{
userData = (UserData)Application["UserData_" + authTicket.Name];
}
Context.User = new GenericPrincipal(new GenericIdentity(authTicket.Name), userData.Roles);
}
的UserData是我創建的用於處理高速緩存用戶角色的類。這是因爲活動目錄返回用戶所屬的組成員身份所花費的時間。爲了完整性,以下是我擁有的UserData代碼。
public class UserData
{
private int _TimeoutInMinutes;
private string[] _Roles = null;
public string UserName { get; private set; }
public DateTime Expires { get; private set; }
public bool Expired { get { return Expires < DateTime.Now; } }
public string[] Roles
{
get
{
if (Expired || _Roles == null)
{
_Roles = GetADContainingGroups(UserName).ToArray();
Expires = DateTime.Now.AddMinutes(_TimeoutInMinutes);
}
return _Roles;
}
}
public UserData(string userName, int timeoutInMinutes = 20)
{
UserName = userName;
_TimeoutInMinutes = timeoutInMinutes;
}
}
您是否嘗試過轉儲當前用戶所在的組/聲明列表?我懷疑他們在那裏,但作爲AD組SID不是組名。 – 0leg