Web應用程序,用戶可以在其中內部或外部查看其SSRS報告。試圖根據AD對用戶進行身份驗證 - ASP.NET MVC
我正在嘗試使用自定義授權角色對活動目錄組進行登錄身份驗證,因此可以根據它們是否位於特定AD組中來確保報告的安全性。
現在我知道這與Windows身份驗證/表單身份驗證,但由於其他原因使用自定義身份驗證,但我所擁有的是具有用戶登錄自定義用戶名的表映射到他們的Windows憑據。
我一直在關注這個blog來測試這種針對活動目錄組進行身份驗證的方法,並對它進行了自定義以傳遞映射到自定義用戶登錄的Windows憑據,但迄今爲止沒有運氣。
關於自定義身份驗證,當我從我的表中找到匹配的域名並將該域名存儲到會話變量中時,然後將其傳遞到此AD身份驗證過程中,以檢查用戶是否存在於請參閱下面的代碼。
自定義授權屬性,
using Helpers;
using Models;
using System;
using System.Web;
using System.Web.Mvc;
namespace Application.Validators
{
public class AuthorizeADAttribute : AuthorizeAttribute
{
public string Group { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (string.IsNullOrEmpty(Group))
{
return true;
}
var logOnInfo = httpContext.Session["LogOnInfo"] as LogOnModel;
var username = logOnInfo.DomainName;
try
{
return LDAPHelper.UserIsMemberOfGroups(username, Group);
}
catch (Exception ex)
{
return false;
}
}
}
}
LDAP助手,在web.config
using System;
using System.Configuration;
using System.DirectoryServices.AccountManagement;
using System.Web;
namespace Application.Helpers
{
public class LDAPHelper
{
public static string GetLDAPContainer()
{
Uri ldapUri;
ParseLDAPConnectionString(out ldapUri);
return HttpUtility.UrlDecode(ldapUri.PathAndQuery.TrimStart('/'));
}
public static bool ParseLDAPConnectionString(out Uri ldapUri)
{
string connString =
ConfigurationManager.ConnectionStrings["ADConnectionString"]
.ConnectionString;
return Uri.TryCreate(connString, UriKind.Relative, out ldapUri);
}
public static bool UserIsMemberOfGroups(string username, string Group)
{
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(Group))
{
return false;
}
// Verify that the user is in the given AD group (if any)
using (var context = BuildPrincipalContext())
{
var userPrincipal = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
username);
return userPrincipal.IsMemberOf(context, IdentityType.Name, Group);
}
}
public static PrincipalContext BuildPrincipalContext()
{
string container = GetLDAPContainer();
return new PrincipalContext(ContextType.Domain, null, container);
}
}
}
LDAP連接字符串(可以確認是正確的),
<add name="ADConnectionString" connectionString="LDAP://CN=Managers;OU=Groups,OU=Users,DC=domain"/>
我的問題,我想當我試圖從LDAP連接字符串返回容器(GetLDAPHelper方法)回到Princip alContext它只是返回null並拋出一個錯誤。
我正在查看是否有人做了任何遠程類似的事情,或者有沒有更適合的方法來嘗試實現我在做什麼?
什麼是錯誤? – raven
當我的GetLDAPContainer方法轉到ParseLDAPConnectionString方法以返回ldapUri時,爲空引用異常。 當我調試並將光標放在connString變量上時,它會正確顯示連接字符串,但不會將其返回給GetLDAPContainer方法。 –
ldapUri.PathAndQuery爲空,這就是爲什麼。 – raven