Membership CreateUser 及相关表

发布于 2024-12-20 05:43:29 字数 690 浏览 3 评论 0原文

我刚刚切换到 .Net 会员提供商来管理我的用户。我有一个与用户表相关的表,名为UserGroupDetails。我想在调用 Membership.CreateUser 时向该表添加一行。如果添加用户失败,我想确保 UserGroupDetails 中的记录被删除或不添加,如果将记录添加到 UserGroupDetails 失败,我想 Membership.CreateUser 失败并且不会添加用户。

我对使用 SqlProfileProvider 并不真正感兴趣,并且没有找到任何有关覆盖或修改 Membership.CreateUser 来执行此附加插入的信息。

是否有可能,如果可以的话,有人可以向我指出如何实现这一目标的良好资源吗?

另外,我不想重写整个 Membership.CreateUser 方法。相反,我想简单地添加它。我查看了 http://msdn.microsoft.com/en-us/library /ms366730.aspx 但这似乎是在重建轮子。

谢谢, D

I have just switch to .Net membership provider to manage my users. I have a table relating to the Users table called UserGroupDetails. I would like to add a row to this table when Membership.CreateUser is called. If adding the user fails i would like to make sure that the record in UserGroupDetails is removed or not added and if adding the record to UserGroupDetails fails i would like the Membership.CreateUser to fail and the user to not be added.

I am not really interested in using the SqlProfileProvider and i have not found any information about overriding or modify the Membership.CreateUser to perform this additional insert.

Is it possible and if so could someone point me to a good resource on how to accomplish this?

Also, i do not want to rewrite the whole Membership.CreateUser method. Rather i would like to simple add to it. I have looked at http://msdn.microsoft.com/en-us/library/ms366730.aspx but this seems to be rebuilding the wheel.

Thanks,
D

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

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

发布评论

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

评论(4

看轻我的陪伴 2024-12-27 05:43:29

您将构建一个自定义提供程序

You would build a custom provider

我已经做到了!我首先创建我的 aspnet 用户。这就是我正在做的事情。

1)我将其添加到我的web.config中:

<system.web>
        <membership>
            <providers>
                <clear/>
                <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
                     enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
                     maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
                     applicationName="[ApplicationName]" />
            </providers>
        </membership>
        <machineKey validationKey="[]" decryptionKey="[]" decryption="3DES" validation="SHA1" />
 <connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=(local);Initial Catalog=[DataBaseName];User ID=[UserName];Password=Password" providerName="System.Data.SqlClient" />
 </connectionStrings>
 </system.web>

2)然后我用它来创建我的用户表

   var user = Membership.CreateUser(request.UserName, request.Password, request.Email);

3)最后我获取我刚刚创建的aspnet用户id并将其与我的自定义用户关联

user.ProviderUserKey.ToString();

您可以创建一些逻辑来删除aspnet如果插入 UserGroupDetails 失败。

I've done this! I start by creating my aspnet user first. Here's what I'm doing.

1) I added this to my web.config:

<system.web>
        <membership>
            <providers>
                <clear/>
                <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
                     enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
                     maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
                     applicationName="[ApplicationName]" />
            </providers>
        </membership>
        <machineKey validationKey="[]" decryptionKey="[]" decryption="3DES" validation="SHA1" />
 <connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=(local);Initial Catalog=[DataBaseName];User ID=[UserName];Password=Password" providerName="System.Data.SqlClient" />
 </connectionStrings>
 </system.web>

2) I then use this to create my user table

   var user = Membership.CreateUser(request.UserName, request.Password, request.Email);

3) Finally I grab my aspnet user id I just created and relate it in my custom user

user.ProviderUserKey.ToString();

You could create some logic to delete the aspnet user if insert into UserGroupDetails fails.

神妖 2024-12-27 05:43:29

您不需要构建自定义提供程序。只需在调用 CreateUser 后添加代码来连接 UserGroupDetails 表即可。

不幸的是,如果出现故障,您无法使用 TransactionScope 来回滚事务,因为 CreateUsre 将使用与代码分开的数据库连接,这将需要将事务提升为分布式事务,这配置和设置起来很痛苦。

最简单的解决方案是仅检查 CreateUser 是否失败,如果失败则不执行第二位代码,如果第二位失败则删除创建的用户。然而,这并不是万无一失的,因为在某些情况下您可能会有孤立的记录。

为了在单个事务中做到这一点,唯一的解决方案是自定义提供程序,但我个人认为这不值得付出努力。

You don't need to build a custom provider. Just add your code to hookup your UserGroupDetails table after you call CreateUser.

Unfortunately, you can't use a TransactionScope to rollback the transaction if something fails because the CreateUsre will use a seperate database connection from your code, and this will require promoting the transaction to a distrivuted transaction, which is a pain to configure and setup.

The easiest solution is just check for failure of the CreateUser, and don't execute the second bit of code if it fails, and deleting the created user if your second bit fails. However, this is not foolproof as there are situations where you might have an orphaned record.

In order to do this in a single transaction, then the only solution is a custom provider, but I personally don't think it's worth the effort.

甜中书 2024-12-27 05:43:29

创建一个继承membershipprovider的自定义提供程序。重写创建用户方法以扩展功能。调用 base.CreteUser,然后添加代码以将其他记录添加到自定义表中。

更新:

这是您的自定义提供程序的样子

using System.Web.Security;

namespace YourNameSpace
{

    public class CustomSqlMembershipProvider : SqlMembershipProvider
    {

        public bool CreateUser(//list of parameters)
        {
            bool UserCreated = false;
            MembershipCreateStatus status;

            //provide the required parameters 
            base.CreateUser(//list of parameters);

            if (status == 0)//user was successfully created
            { 
                //perform your custom code
                //if success 
                UserCreated = true;
            }

            return UserCreated
        }
    }
}

确保您在 Web.config 文件中引用新的自定义提供程序

   <membership defaultProvider="CustomMembeshipProvider">
      <providers>
        <clear/>
        <add connectionStringName="Your connection string name" name="CustomMembeshipProvider"
             type="YourNameSpace.CustomSqlMembershipProvider" />
      </providers>
    </membership>

Create a custom provider that inherits membershiprovider. Override the create user method to extend the functionality. Call base.CreteUser and then add your code for adding the additional records to your custom table.

UPDATE:

Here's what your custom provider would look like

using System.Web.Security;

namespace YourNameSpace
{

    public class CustomSqlMembershipProvider : SqlMembershipProvider
    {

        public bool CreateUser(//list of parameters)
        {
            bool UserCreated = false;
            MembershipCreateStatus status;

            //provide the required parameters 
            base.CreateUser(//list of parameters);

            if (status == 0)//user was successfully created
            { 
                //perform your custom code
                //if success 
                UserCreated = true;
            }

            return UserCreated
        }
    }
}

Make sure you reference your new custom provider in your Web.config file

   <membership defaultProvider="CustomMembeshipProvider">
      <providers>
        <clear/>
        <add connectionStringName="Your connection string name" name="CustomMembeshipProvider"
             type="YourNameSpace.CustomSqlMembershipProvider" />
      </providers>
    </membership>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文