创建自定义成员资格提供程序的示例代码 [英] Sample Code for Creating Custom Membership Provider

查看:30
本文介绍了创建自定义成员资格提供程序的示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 MVC 3 应用程序,我正在尝试实现我自己的自定义成员资格提供程序(遵循 Apress 的 Pro ASP.NET MVC 3 框架中的示例).

I am writing an MVC 3 application and I am trying to implement my own custom membership provider (following the sample in Apress' Pro ASP.NET MVC 3 Framework).

我创建了自定义类,继承自 MembershipProvider 并(使用 ReSharper)使用 NotImplementedExceptions 实现了所有 27 种方法.

I created my custom class, inherited from MembershipProvider and (using ReSharper) implemented all 27 of the methods with NotImplementedExceptions.

现在,我已经按照书中所述重写了 ValidateUser 方法并且它工作正常,但我想让我的提供者更加健壮并实现其他方法(例如设置 MinRequiredPasswordLength 和 GetNumberOfUsersOnline).

Now, I have overridden the ValidateUser method as the book states and it works fine, but I would like to make my provider more robust and implement the other methods (e.g. set MinRequiredPasswordLength and GetNumberOfUsersOnline).

是否有一些示例代码可以用来开始填充这些方法,我可以调整这些方法以适应我自己的数据库/模型架构?

Is there some sample code that I can use to start populating these methods that I can tweak to fit my own DB/Model schema?

我当然可以通过反复试验来解决这个问题,但基本代码示例会大有帮助.

I can certainly use trial and error to figure it out, but a base code sample would greatly help.

这个问题只是被低估了两次.如果您要投反对票,请发表评论以说明原因,以便我可以改进我的问题.

This question was just downvoted twice. If you are going to downvote, please post a comment as to why so that I can work on improving my questions.

编辑 2:例如,对于以下方法:

public override int GetNumberOfUsersOnline()
{
  throw new System.NotImplementedException();
}

我可以尝试从头开始编写代码来查看一些网络日志,确定用户登录时间并估计他们是否还在,但这需要我花费大量时间来弄清楚.由于所有这些代码都来自 Microsoft 为标准 SqlMembershipProvider 编写的同一接口,是不是有包含此方法的代码(甚至来自 MS)?如果是这样,我想接受它,修改它以便它使用我的数据库架构而不是 aspnetdb 默认架构.我更喜欢使用某种基本代码.我认为这将是一个简单且相当标准的请求,但也许不是.

I can try to write code from scratch to look at some web log, determine the users login time and approximate if they are still on, but that will take a large amount of time for me to figure out. Since all this code is from the same interface that Microsoft wrote for the standard SqlMembershipProvider, isn't there code out there (even from MS) that contains this method? If so, I want take it, modify it so it uses my DB schema instead of the aspnetdb default schema. I would prefer to have some sort of base code to work from. I thought this would be a simple and fairly standard request, but perhaps not.

推荐答案

您不能为您的自定义提供程序使用默认提供程序的代码,这就是您实现自定义提供程序的原因,根据您的要求对其进行调整,使用你自己的数据库表等

You can't use the default provider's code for your custom provider, that is why you are implementing a custom one, to tweak it according to your requirements, use your own db tables etc.

看看我关于自定义会员资格的博文, 自定义角色 提供者和 自定义会员用户.那里有一个示例,说明如何使用自己的数据库来获取/设置成员资格信息.

Take a look at my blog posts about custom membership, custom role providers and custom membership user. There is an example there of how you can use your own database to get/set membership information.

这篇关于创建自定义成员资格提供程序的示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆