Asp.net mvc 4 具有Windows身份验证的自定义成员资格提供程序

Asp.net mvc 4 具有Windows身份验证的自定义成员资格提供程序,asp.net-mvc-4,membership-provider,Asp.net Mvc 4,Membership Provider,我已经创建了自己的会员资格提供商,其中我有以下方法: public override bool ValidateUser(string username, string password) { if (username == "John") return true; else return false; } 我还在web.config文件中添加了以下行: <authentication mode="Windows" /> &l

我已经创建了自己的会员资格提供商,其中我有以下方法:

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
派生控制器。例如,您可以定义一个类,如:

[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
{
}