当表User没有Id时,如何创建自定义简单成员资格提供程序(How to create custom simple membership provider when table User doesn&

编程入门 行业动态 更新时间:2024-10-28 20:28:01
当表User没有Id时,如何创建自定义简单成员资格提供程序(How to create custom simple membership provider when table User doesn't have Id)

我正在尝试在ASP.NET MVC 4中创建一个Custom Simple Membership Provider 。在InitializeSimpleMembershipAttribute类中,我有这行代码:

WebSecurity.InitializeDatabaseConnection("ConnStringForWebSecurity", "User", "", "Username", autoCreateTables: false);

正如您在此处看到的,我尝试在我的数据库中使用User表。 对于InitializeDatabaseConnection函数:

第一个参数是连接字符串名称。 第二个是表名。 第三个是用户ID列。 第四个是用户名栏。

此函数需要我们为User表提供一个id列。 但就我而言。 我使用Username列作为主键,所以我没有id列。

我的问题是,如果我没有id列,我应该传递给第三个参数? 不允许传递null或空字符串,并抛出异常。 我该怎么办?

非常感谢你的帮助。

I'm trying to create a Custom Simple Membership Provider in ASP.NET MVC 4. In InitializeSimpleMembershipAttribute class, I have this line of code:

WebSecurity.InitializeDatabaseConnection("ConnStringForWebSecurity", "User", "", "Username", autoCreateTables: false);

As you can see here, I try to use User table in my database. For InitializeDatabaseConnection function:

The first parameter is the connection string name. The second one is table name. The third one is user id column. The fourth one is username column.

This function expects that we have an id column for User table. But in my case. I use Username column as the primary key, so I don't have id column.

My question is, if I don't have id column, what should I pass to the third parameter? Passing null or empty string is not allowed and exception will be thrown. What should I do?

Thanks a lot for your help.

最满意答案

简短回答 -

您必须在用户配置文件表中具有int类型的ID列(根据需要命名)。 SimpleMembershipProvider (SMP)在UserProfile表中强制要求两个非空的第一列ID和第二个用户名。 这是当你选择从SimpleMembershipProvider派生时,在你的情况下我怀疑你真的需要。 如果您有任何自定义需求( SimpleMembershipProvider阻止您实现),您可以从MembershipProvider派生。

抛出异常是因为, SimpleMembershipProvider在内部使用UserId列并且假定它不为null。

更详细一点 -

现在再详细一点,帮助您了解原因 -

SimpleMembershipProvider设计灵活,但其主要目标是提供一种更简单的验证用户的方法。 它的重点是针对具有常见身份验证需求的80%的Web应用程序。

例如,如果要自定义,要在UserProfile表中添加自定义列(电子邮件)(表名也是自定义),则可以这样做。 SimpleMembershipProvider只是希望您创建一个数据库,它可以为您创建表,但该表必须有两个必填列(UserID和UserName)。

有关更多信息,请参阅@miller在评论中分享的链接。

如果您有任何跟进问题,请通过创建自定义提供商分享您尝试实现的更多详细信息。

编辑(回答后续问题)

列的名称可以是任何内容(EmployeeId也可以),它将在数据库表中创建为非null int类型。 只要您没有调用使用UserId的方法,您的应用程序就会起作用。 我想知道你是如何创建一个新用户的。 使用新的MVC 4 Web应用程序,当您注册新用户时, WebSecurity调用转发给SimpleMembershipProvider以创建新用户。 SimpleMembershipProvider的CreateUserRow()方法检查UserID是否存在。 如果已存在则抛出异常。 创建用户后, SimpleMembershipProvider将UserId用于多个公共方法,例如GetUserNameFromId(),GetUser(),DeleteUser(),CreateAccount()等。

会员使用UserProfile表的方式,恐怕你将不得不提供UserId列。 您也可以使用UserName检索用户,并且根本不会使用UserId。

当您使用SimpleMembershipProvider考虑将UserProfile表的两列保留为内部使用。 可以通过添加更多列来构建任何自定义应用程序逻辑。

Short answer -

You have to have ID column (name it as you like) of type int in the user profile table. SimpleMembershipProvider (SMP) mandates two non null column one ID and second user name in the UserProfile table. This is when you choose to derive from SimpleMembershipProvider which in your case I doubt you really need to. If you have any custom needs (which SimpleMembershipProvider stops you to achieve), you can derive from MembershipProvider instead.

The exception is thrown because, SimpleMembershipProvider uses the UserId column internally and it assumed to be not null.

A bit more detail -

Now for a bit more detail to help you understand why so -

SimpleMembershipProvider has been designed to be flexible however its key objective is to provide a simpler way of validating user. Its focus is to target those 80% of the web application that has common Authentication needs.

If you want to customize, for example, you want to add a custom column (email) in your UserProfile table (the table name is customization as well), you can do so. SimpleMembershipProvider simply expects you to have a database created, it can create table for you but the table has to have two mandatory columns (UserID and UserName).

For more information see the link shared in comment by @miller.

If you have any follow up question please share with a bit more details about what you are trying to achieve by creating a custom provider.

Edit (Answer to follow up question)

The name of the column can be anything (EmployeeId will work as well), it will be created as non null int type in database table. Your application will work as long as you are not calling a method that make use of UserId. I wonder how you are creating a new user. With a new MVC 4 web application, when you register a new user, WebSecurity forward the call to SimpleMembershipProvider to create a new user. SimpleMembershipProvider's CreateUserRow() method checks UserID exist or not. If already exist throw an exception. Once the user is created SimpleMembershipProvider uses UserId for several public method e.g. GetUserNameFromId(), GetUser(), DeleteUser(), CreateAccount(), etc.

The way Membership uses UserProfile table, I am afraid you will have to provide a UserId column. You can retrieve user by using UserName as well and will not use UserId at all.

When you use SimpleMembershipProvider consider UserProfile table's two columns as reserved for internal usage. Any custom application logic can be build by adding more column.

更多推荐

本文发布于:2023-07-26 02:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   成员   资格   简单   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!