我需要对用户进行身份验证和授权的最低 ASP.NET 提供程序实现是什么?

发布于 2024-10-17 06:17:18 字数 474 浏览 8 评论 0原文

默认情况下,ASP.NET MVC 将 AccountController 设置为使用 SqlMembershipProvider、SqlProfileProvider 和 SqlRoleProvider。我真的不需要所有的东西,事实上,将我的数据塑造成该模型更麻烦。

为了获得身份验证和授权并且不破坏可能存在的其他依赖关系,我至少需要在 MembershipProvider、RoleProvider 和 ProfileProvider 抽象类上实现什么?

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

此外,例如在 MembershipProvider 上,我不需要 FindUsersByEmail 方法。如果我不实现它,ASP.NET MVC 会在某个时刻卡住吗?如果是的话,在哪里?

By default ASP.NET MVC setups up the AccountController to use the SqlMembershipProvider, SqlProfileProvider and the SqlRoleProvider. I don't really need everything that brings to the table, in fact, it is more of a hassle to shape my data into that model.

What is the minimum I need to implement on the MembershipProvider, RoleProvider and ProfileProvider abstract classes to get authentication and authorization and not break some other dependency that might be there?

For instance, on the ProfileProvider it wants me to override the "FindInactiveProfilesByUserName" method, but I don't really care about this feature. Where is my app going to break when the NotImplementedException fires?

Additionally, on the MembershipProvider for instance, I don't need the FindUsersByEmail method. If I don't implement it will ASP.NET MVC choke at some point? If so, where?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ偶尔ゞ 2024-10-24 06:17:18

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

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

我出于类似的原因实现了自定义提供程序(我不喜欢 sql 提供程序使用的架构),但我选择不实现自定义配置文件提供程序,因为它似乎依赖于配置文件属性的配置设置,并且我不希望走这条路(请参阅ASP.NET 配置文件属性概述)。

附带说明一下,我发现在实现自己的提供程序时,查看 Reflector 中的 SqlMembershipProviderSqlRoleProvider 很有帮助,因此您可能也想这样做。

As far as I know, ASP.NET MVC doesn't really do anything for you with regard to authentication. With that in mind, as @chrispr says, you should only need to implement ValidateUser, and the project created by the ASP.NET MVC project template only calls that method during authentication.

Regarding authorization, I took a look at AuthorizationAttribute in Reflector and found that it calls IPrincipal.IsInRole. Looking at System.Web.Security.RolePrincipal in Reflector, IsInRole calls GetRolesForUser, so you could try implementing only that method to start with.

I implemented custom providers for similar reasons (I don't like the schema the sql providers use), but I chose not to implement a custom profile provider since it seems to rely on configuration settings for the profile properties, and I didn't want to go that route (see ASP.NET Profile Properties Overview).

As a side note, I found that looking at the SqlMembershipProvider and SqlRoleProvider in Reflector was helpful when I implemented my own providers, so you might want to do the same.

尤怨 2024-10-24 06:17:18

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

I believe you only need to implement ValidateUser on the MembershipProvider to take advantage of the authentication features of the MembershipProvider. The rest of the features are invoked by provided web controls like CreateUserWizard, so make sure to disable any of the unsupported features on those controls when using them. As for the rest (RoleProvider and ProfileProvider), if you're not using any of the features related to User Roles or User Profiles, you don't have to implement any of the members.

满身野味 2024-10-24 06:17:18

这是我的自定义提供程序中的内容:

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();
        }
    }
}

Here's what I have in my custom providers:

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)
        {
            //
        }
    }
}

and:

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文