获得用户身份验证和授权所需的最低ASP.NET提供程序实现是什么?

时间:2011-02-12 15:50:59

标签: asp.net asp.net-mvc asp.net-membership asp.net-authentication

默认情况下,ASP.NET MVC设置AccountController以使用SqlMembershipProvider,SqlProfileProvider和SqlRoleProvider。我并不真正需要提供所有内容的东西,事实上,将数据整合到该模型中更麻烦。

我需要在MembershipProvider,RoleProvider和ProfileProvider抽象类上实现最小化以获取身份验证和授权,而不是破坏可能存在的其他依赖项?

例如,在ProfileProvider上,它希望我覆盖“FindInactiveProfilesByUserName”方法,但我并不真正关心此功能。当NotImplementedException触发时,我的应用程序将在哪里中断?

此外,例如,在MembershipProvider上,我不需要FindUsersByEmail方法。如果我没有实现它,ASP.NET MVC会在某些时候窒息吗?如果是这样,在哪里?

3 个答案:

答案 0 :(得分:3)

我相信您只需要在MembershipProvider上实现ValidateUser即可利用MembershipProvider的身份验证功能。其余功能由提供的Web控件(如CreateUserWizard)调用,因此请确保在使用这些控件时禁用这些控件上的任何不受支持的功能。至于其余部分(RoleProvider和ProfileProvider),如果您没有使用与用户角色或用户配置文件相关的任何功能,则不必实现任何成员。

答案 1 :(得分:3)

据我所知,ASP.NET MVC在身份验证方面并没有真正为您做任何事情。考虑到这一点,正如@chrispr所说,您应该只需要实现ValidateUser,并且ASP.NET MVC项目模板创建的项目仅在身份验证期间调用该方法。

关于授权,我查看了Reflector中的AuthorizationAttribute,发现它调用IPrincipal.IsInRole。查看Reflector中的System.Web.Security.RolePrincipalIsInRole调用GetRolesForUser,因此您可以尝试仅使用该方法开始。

我出于类似的原因实现了自定义提供程序(我不喜欢sql提供程序使用的模式),但我选择不实现自定义配置文件提供程序,因为它似乎依赖于配置文件属性的配置设置,而且我没有我不想走那条路(见ASP.NET Profile Properties Overview)。

作为旁注,我发现在我实现自己的提供程序时,查看Reflector中的SqlMembershipProviderSqlRoleProvider会很有帮助,所以您可能也希望这样做。

答案 2 :(得分:0)

以下是我在自定义提供程序中的内容:

namespace MyProject
{
    public class SqlMembershipProvider : System.Web.Security.SqlMembershipProvider
    {
        private string ConnectionString { get; set; }

        public override bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            //
        }

        public overrideMembershipUser CreateUser(string userName, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            //
        }

        private MembershipUser CreateUser(string userName, string password, object providerUserKey, out MembershipCreateStatus status)
        {
            //
        }

        public override bool DeleteUser(string userName, bool deleteAllRelatedData)
        {
            //
        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            this.ConnectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;

            base.Initialize(name, config);
        }

        public override MembershipUser GetUser(string userName, bool userIsOnline)
        {
            //
        }

        public override bool ValidateUser(string userName, string password)
        {
            //
        }
    }
}

namespace MyProject
{
    public class SqlRoleProvider : System.Web.Security.RoleProvider
    {
        private string ConnectionString { get; set; }

        public override void AddUsersToRoles(string[] userNames, string[] roleNames)
        {
            //
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotSupportedException();
            }
            set
            {
                throw new NotSupportedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotSupportedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotSupportedException();
        }

        public override string[] FindUsersInRole(string roleName, string userNameToMatch)
        {
            throw new NotSupportedException();
        }

        public override string[] GetAllRoles()
        {
            //
        }

        public override string[] GetRolesForUser(string userName)
        {
            //
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotSupportedException();
        }

        public override bool IsUserInRole(string userName, string roleName)
        {
            //
        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            this.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;

            base.Initialize(name, config);
        }

        public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
        {
            throw new NotSupportedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotSupportedException();
        }
    }
}
相关问题