【问题标题】:Custom Membership Provider with Windows authetication具有 Windows 身份验证的自定义成员资格提供程序
【发布时间】:2015-04-10 12:53:25
【问题描述】:

我已经创建了自己的 Membership Provider,我有以下方法:

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],这样只有我数据库中的用户才能真正访问该页面。

【问题讨论】:

  • 您尝试过我下面建议的解决方案吗?你的问题解决了吗?

标签: asp.net-mvc-4 membership-provider


【解决方案1】:

您可以定义自己的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
{
}

【讨论】:

    猜你喜欢
    • 2011-06-27
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    相关资源
    最近更新 更多