.Net 中的自定义成员资格有什么好处?

发布于 2024-11-25 14:51:42 字数 1467 浏览 3 评论 0原文

我正在 .net 中建立自定义会员资格,但我不明白它们的真正好处,有什么好处?这是我继承自 MembershipProvider 的类:

public class MembershipService : MembershipProvider
{
  public override bool ValidateUser(string username, string password)
  {
    Credencial credencial = context.Credencial.Where(x => x.Usuario == username).FirstOrDefault();

    if (credencial == null) return false;

    if (credencial.Senha == password)
    {
      return true;
    }

    return false;
  }
}

这是我在 web.config 中的配置:

<membership defaultProvider="MembershipService" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add
      name="MembershipService"
      type="Questiona2011.Services.MembershipService" />
  </providers>
</membership>

这是我使用它们的地方:

[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(indexViewModel.Usuario, indexViewModel.Senha))
        {
            FormsAuthenticationService.SignIn(indexViewModel.Usuario, false);
            return RedirectToAction("Index", "Default");
        }
    }

    return View();
} 

我不明白,这个类 MembershipService 我不需要继承MembershipProvider

使用继承自 MembershipProvider 的自定义会员资格有什么真正的好处?

为什么我需要继承MembershipProvider

i'm building a custom membership in .net and i don't understand real benefits of them, what the benefits? Here is my class that inherits from MembershipProvider:

public class MembershipService : MembershipProvider
{
  public override bool ValidateUser(string username, string password)
  {
    Credencial credencial = context.Credencial.Where(x => x.Usuario == username).FirstOrDefault();

    if (credencial == null) return false;

    if (credencial.Senha == password)
    {
      return true;
    }

    return false;
  }
}

And that's my configuration in web.config:

<membership defaultProvider="MembershipService" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add
      name="MembershipService"
      type="Questiona2011.Services.MembershipService" />
  </providers>
</membership>

Here's where i use them:

[HttpPost]
public ActionResult Index(IndexViewModel indexViewModel)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(indexViewModel.Usuario, indexViewModel.Senha))
        {
            FormsAuthenticationService.SignIn(indexViewModel.Usuario, false);
            return RedirectToAction("Index", "Default");
        }
    }

    return View();
} 

I don't understand, this class MembershipService i don't need to inherits from MembershipProvider.

What's the real benefits using a Custom Membership that inherits from MembershipProvider?

Why I need to inherit from MembershipProvider?

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

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

发布评论

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

评论(3

忆依然 2024-12-02 14:51:42

如果您想知道为什么,您必须从框架的基类继承;也许您可以查看文档。具体来说,它表示您必须继承抽象类 MembershipProvider,该类已经继承了 ProviderBase。因此,与所有抽象类一样,它必须提供一些通用功能的实现(最有可能与配置文件中的配置有关)。

自行部署的主要好处是改变框架实现的行为(例如,连接到现有的数据库模式)。这样做使您能够使用会员控件 - 包括登录、LoginView、CreateUserWizard、ChangePassword 等。

If you want to know why you must inherit from the framework's base class; perhaps you might check the documentation. Specifically, it says that you must inherit the abstract class MembershipProvider which already inherits ProviderBase. Therefore, as with all abstract class, it must therefore provide some implementation for common functionality (most likely to do with configuration into the config files).

The main benefit for rolling your own is to alter the behaviour of the framework's implementations (for example, to connect to your existing database schema). Doing so enables you to make use of the Membership controls - including Login, LoginView, CreateUserWizard, ChangePassword, etc.

凤舞天涯 2024-12-02 14:51:42

如果您已经在创建自定义会员资格,您可能知道为什么要这样做。

如果您在注册中需要其他字段,您将构建自定义字段。通常,当您创建自定义成员资格提供程序时,该提供程序使用与标准提供程序使用的预定义表分开的自定义 SQL Server 表。

If you are already creating custom memebership you might be knowing why you are doing it .

If you need additional fields in your registration you will build custom one . Generally when you create a custom membership provider that uses custom SQL Server tables separate from the pre-defined tables used by the standard provider.

痴意少年 2024-12-02 14:51:42

我曾经使用过它,有一天醒来..这个继承有什么收获?

答案是我在我的场景中没有获得任何收获。从今天起,我不再使用会员资格,我创建了一个我认为最适合我的现实的表结构和类。我并不后悔。

总体会员资格是为了避免重新发明轮子......这些人仍然认为它是稳定的并且已经工作了多年。任何不能带来同样保证的解决方案。 E 由于绝大多数来自使用 Membership 的 Webform,因此在 MVC 中继续使用相同的内容。

我的观点是,你不会通过这份遗产赢得任何东西。实际上,这就像您会使用给定的接口,因为它需要实现所有代码!除非与带有登录控件的 Web 表单控件一起使用,如果我有会员类,则无需编码,但 MVC 是我没有控件的情况。

无论如何,我要说的就是这个。我从来没有遇到过使用自定义成员资格(带有继承)会更好的情况! FormsAuthentication 类、RoleProvider 类和我的自定义身份验证类完成身份验证和授权工作。

For a time I used it and one day woke up.. what the gain with this inheritance?

The answer is that I got in my scenarios had no gain. From this day forward I no longer use the Membership, I created a table structure and classes I found most appropriate to my reality. And I do not regret.

Overall Membership is indicated to avoid having to reinvent the wheel... the guys still argues that it is stable and has worked years. Any solution that does not bring out this same guarantee. E as the great majority come from webforms using Membership, continued using the same in the MVC.

MY opinion is that you do not win anything with this inheritance. In real it's like you would use a given interface, since it needs implement all the code! Unless using with Web Forms Controls with the Login Controls where i no need to code if I had a Membership Class, but MVC is my scenario where I have no controls.

Anyway, I have to say is this. I've never had a situation that use custom membership (with inheritance) would be better! FormsAuthentication class, RoleProvider class and my custom Authentication class do the work with authentication and authorization.

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