我有一個MVC3應用程序具有存儲在數據庫中的自定義成員資格提供程序和用戶/角色。用戶根據需要在應用程序中手動創建,並分配適當的角色。MVC3自定義MembershipProvider和活動目錄
我想現在擴展應用程序,以提供使用Active Directory的選項,但因爲應用程序有幾個自定義字段+表用戶FK查找,我想我仍然必須有一個定製版本的默認活動目錄成員資格提供程序。
SF上有人做過類似的事情,他們可以與我分享? 感謝
我有一個MVC3應用程序具有存儲在數據庫中的自定義成員資格提供程序和用戶/角色。用戶根據需要在應用程序中手動創建,並分配適當的角色。MVC3自定義MembershipProvider和活動目錄
我想現在擴展應用程序,以提供使用Active Directory的選項,但因爲應用程序有幾個自定義字段+表用戶FK查找,我想我仍然必須有一個定製版本的默認活動目錄成員資格提供程序。
SF上有人做過類似的事情,他們可以與我分享? 感謝
我知道這是一個老問題,但...
讓我們看看從哪裏開始
在我的web應用程序我建立了直接基於身份驗證與我的ADFS服務器的聯合聲明。我一直無法找到一個很好的教程,如何做到這一點,因爲它不是微不足道的。但是有很多關於如何使用azure ACS作爲中間人來做到這一點的參考資料。這其中將至少讓你開始:
http://haishibai.blogspot.com/2011/05/tutorialaspnet-mvc-3-claim-based.html
一旦你得到這個工作,你只需要幾件事情。
在您的數據庫用戶表中添加一些可以與AD鏈接的屬性。我將AD GUID存儲在我的,但我也使用電子郵件地址作爲輔助。這使我可以在我的應用程序中創建用戶,然後讓他們通過AD進行身份驗證。我只是將他們的電子郵件作爲聲明傳回,在我的應用中將其與用戶匹配,然後將AD GUID添加到用戶。
我也利用繼承來做我的身份驗證。我的所有控制器都從BaseController繼承,所以他們獲得了這種標準的行爲。
public class BaseController
{
protected override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//read in the claims that we got back from ADFS
IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;
IClaimsIdentity ici = icp.Identity as IClaimsIdentity;
var claims = ici.Claims;
// This is a claim that I add manually to see if I've already synced
// ADFS user with DB user
var ppid = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.PPID);
if (ppid == null)
{
//query/sync user.
var guidString = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Name).Value;
// get AD GUID
var userGuid = new Guid(System.Convert.FromBase64String(guidString));
//look up user
var currentUser = UserRepository.FetchUserByGUID(userGuid);
//if user not found try fetch by email.
if (currentUser == null)
{
var email = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Email).Value;
currentUser = UserRepository.FetchByEmail(email);
}
//If user is still not found create User
if (currentUser == null)
{
currentUser = new Models.User();
BaseRepository.GetDataContext().Users.Add(currentUser);
}
//update users information using AD claim as master record
currentUser.ADID = userGuid;
currentUser.Name = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.GivenName).Value;
currentUser.EmailAddress = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Email).Value;
currentUser.LastLoginDate = DateTime.UtcNow;
currentUser.LoginCount = currentUser.LoginCount + 1;
BaseRepository.GetDataContext().SaveChanges();
// Now that you have your AD user linked to your user record
// in your database...
// Create new claims in your ADFS token that include all the roles that
// your user has. That way you can just piggyback on claims based
// authentication
foreach (var r in currentUser.Roles)
{
claims.Add(new Claim(ClaimTypes.Role, r.Name));
}
// Add userid claim so that we know that this users claims have already
// been sync with my database
claims.Add(new Claim(ClaimTypes.PPID, currentUser.Id.ToString()));
}
}
base.OnAuthorization(filterContext);
}
希望有所幫助!
碰到這個問題 – SimpleUser 2012-01-16 08:35:52
您是否找到了解決方案? – 2012-11-22 11:05:44