具有 Windows 身份验证的自定义成员资格提供者
Custom Membership Provider with Windows authetication
我已经创建了我自己的会员提供者,我有以下方法:
public override bool ValidateUser(string username, string password)
{
if (username == "John")
return true;
else
return false;
}
我还在 web.config 文件中添加了以下行:
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<membership defaultProvider="MembershipProviter">
<providers>
<clear />
<add name="cls_MembershipProvider" type="App.cls_MembershipProvider"
enablePasswordRetrieval="false"
enablePasswordReset="false"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="App"
/>
</providers>
</membership>
您可能会注意到我正在使用 Windows 身份验证,但我没有登录页面。默认情况下,Active Directory 中的所有用户都可以访问该页面。我的目标是检查用户是否存在于我的数据库中。
在我搜索的所有地方,都有登录页面,其中启动了 ValidateUser。我的问题是我应该在哪里实现 ValidateUser 方法,因为我没有登录页面。我只想控制每个 Controler 方法,这样我就可以添加 [Authorize],这样只有我数据库中的用户才能真正访问该页面。
您可以定义自己的 CustomAuthorizeAttribute
派生自 AuthorizeAttribute
。覆盖 OnAuthorization
方法以使用上下文中的详细信息执行验证。在每个控制器之上应用您的自定义过滤器,或定义一个 BaseController
并从 BaseController
派生您的控制器。例如你可以定义一个 class 像:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class RdbiAuthorizationAttribute : AuthorizeAttribute
{
/// <summary>
/// Verifies that the logged in user is a valid organization user.
/// </summary>
/// <param name="filterContext"></param>
public override void OnAuthorization(AuthorizationContext filterContext)
{
Guard.ArgumentNotNull(filterContext, "filterContext");
Guard.ArgumentNotNull(filterContext.Controller, "filterContext.Controller");
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), inherit: true)
|| filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), inherit: true);
if (skipAuthorization)
{
return;
}
if (string.IsNullOrEmpty(filterContext.HttpContext.User.Identity.Name))
throw new AuthenticationException("User must be logged in to access this page.");
var controller = filterContext.Controller as BaseController;
if (controller != null)
{
var user = controller.GetUser();
if (user == null)
{
throw new InvalidOperationException(string.Format("Logged in user {0} is not a valid user", filterContext.HttpContext.User.Identity.Name));
}
}
base.OnAuthorization(filterContext);
}
}
然后你可以这样定义控制器:
[RdbiAuthorization]
public class BaseController : Controller
{
}
public class MyTestController : BaseController
{
}
我已经创建了我自己的会员提供者,我有以下方法:
public override bool ValidateUser(string username, string password)
{
if (username == "John")
return true;
else
return false;
}
我还在 web.config 文件中添加了以下行:
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
<membership defaultProvider="MembershipProviter">
<providers>
<clear />
<add name="cls_MembershipProvider" type="App.cls_MembershipProvider"
enablePasswordRetrieval="false"
enablePasswordReset="false"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="App"
/>
</providers>
</membership>
您可能会注意到我正在使用 Windows 身份验证,但我没有登录页面。默认情况下,Active Directory 中的所有用户都可以访问该页面。我的目标是检查用户是否存在于我的数据库中。 在我搜索的所有地方,都有登录页面,其中启动了 ValidateUser。我的问题是我应该在哪里实现 ValidateUser 方法,因为我没有登录页面。我只想控制每个 Controler 方法,这样我就可以添加 [Authorize],这样只有我数据库中的用户才能真正访问该页面。
您可以定义自己的 CustomAuthorizeAttribute
派生自 AuthorizeAttribute
。覆盖 OnAuthorization
方法以使用上下文中的详细信息执行验证。在每个控制器之上应用您的自定义过滤器,或定义一个 BaseController
并从 BaseController
派生您的控制器。例如你可以定义一个 class 像:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class RdbiAuthorizationAttribute : AuthorizeAttribute
{
/// <summary>
/// Verifies that the logged in user is a valid organization user.
/// </summary>
/// <param name="filterContext"></param>
public override void OnAuthorization(AuthorizationContext filterContext)
{
Guard.ArgumentNotNull(filterContext, "filterContext");
Guard.ArgumentNotNull(filterContext.Controller, "filterContext.Controller");
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), inherit: true)
|| filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), inherit: true);
if (skipAuthorization)
{
return;
}
if (string.IsNullOrEmpty(filterContext.HttpContext.User.Identity.Name))
throw new AuthenticationException("User must be logged in to access this page.");
var controller = filterContext.Controller as BaseController;
if (controller != null)
{
var user = controller.GetUser();
if (user == null)
{
throw new InvalidOperationException(string.Format("Logged in user {0} is not a valid user", filterContext.HttpContext.User.Identity.Name));
}
}
base.OnAuthorization(filterContext);
}
}
然后你可以这样定义控制器:
[RdbiAuthorization]
public class BaseController : Controller
{
}
public class MyTestController : BaseController
{
}