在.NET 4.0中自定义的MembershipProvider [英] Custom MembershipProvider in .NET 4.0

查看:140
本文介绍了在.NET 4.0中自定义的MembershipProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里有几个线程在让这件事情,但他们大多是过时的和其中的参考链接更是不合时宜的。

There are a few threads here at so about this matter but most of them are outdated and the reference links in them are even more outdated.

我得到了这个网站,我需要连接到一个外部SQL Server(MSSQL)与它自己的表结构,使用默认的asp.net成员资格提供结构不是一个选项。该表的布局是非常简单和用户表看起来像这样(这就是所谓的个人)

I got this website which I need to connect to an external sql server (mssql) with it's own table structure, using the default asp.net membership provider structure is not an option. The table layout is really simple and the usertable looks like this (it's called Individuals)

Individuals
- UserGuid (uniqueidentifier/guid, unique)
- Name (varchar)
- Password (varchar)
- HasAccess (tinyint/ 1 or 0)
- DateTime (datetime)
- Log (xml)

所需的功能很简单,就是在登录一个人,其余的是没有必要的:)

The required functionality is simply to log someone in, the rest is not necessary :)

我跟着一些导游,但其中大部分都是过时的,非常复杂。不幸的是,MSDN的例子遵循这个模式和文档不是很好。

I followed some guides but most of them are outdated and very complex. Unfortunately the msdn examples follows this pattern and the documentation is not very good.

因此​​,如果任何人有一些资源展示如何,还是愿意张贴codesamples或类似的在这里我最好的AP preciate它。

So if anyone got some resources showing how to, or are willing to post codesamples or similar here I'd appreciate it.

谢谢!

推荐答案

这真的很简单:


  1. 创建一个新的文件(如果你不使用一个多层次的体系,在项目的模型文件夹),让我们叫 MyMembershipProvider.cs

继承该类 System.Web.Security.MembershipProvider

自动创建所需的方法(周期+空间,在继承类)

automatically create the needed methods (period + space in the inherit class)

完成!

所有方法都将有 NotImplementedException 外,所有你需要做的就是编辑每一个,把你自己的code。例如,我定义了的getUser ,如下所示:

All methods will have the NotImplementedException exception, all you need to do is edit each one and put your own code. For example, I define the GetUser as shown below:

public override MembershipUser GetUser(string username, bool userIsOnline)
{
    return db.GetUser(username);
}

分贝是我加入到类

MyServicesRepository db = new MyServicesRepository();

在那里,你会发现的getUser 方法:

public MembershipUser GetUser(string username)
{
    OS_Users user = this.FindUserByUsername(username);

    if (user == null)
        return
        new MembershipUser(
            providerName: "MyMembershipProvider",
            name: "",
            providerUserKey: null,
            email: "",
            passwordQuestion: "",
            comment: "",
            isApproved: false,
            isLockedOut: true,
            creationDate: DateTime.UtcNow,
            lastLoginDate: DateTime.UtcNow,
            lastActivityDate: DateTime.UtcNow,
            lastPasswordChangedDate: DateTime.UtcNow,
            lastLockoutDate: DateTime.UtcNow);

    return
        new MembershipUser(
            providerName: "MyMembershipProvider",
            name: user.username,
            providerUserKey: null,
            email: user.email,
            passwordQuestion: "",
            comment: "ANYTHING you would like to pass",
            isApproved: true,
            isLockedOut: user.lockout,
            creationDate: user.create_date,
            lastLoginDate: user.lastLoginDate,
            lastActivityDate: user.lastActivityDate,
            lastPasswordChangedDate: user.lastPasswordChangedDate,
            lastLockoutDate: user.lastLockoutDate);
}

这样做对所有使用方法(调试项目,看你需要哪些) - 我只用了一些,不是所有的,因为我真的不关心像 ChangePasswordQuestionAndAnswer DeleteUser

只要确保在你的的web.config 为你添加新成员:

just make sure that in your web.config you add the new Membership as:

<membership defaultProvider="MyMembershipProvider">
  <providers>
    <clear/>
    <add name="MyMembershipProvider" type="Your.NameSpace.MyMembershipProvider" connectionStringName="OnlineServicesEntities"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />
  </providers>
</membership>


您有一个愉快的视频教程从克里斯·佩尔斯(日期为2007年,但仍然主要有效)和code这个为好,虽然视频教程在VB,但让你明白的步骤...


You have a nice Video Tutorial from Chris Pels (dated 2007 but still mostly valid) and code for this as well, though Video Tutorial is in VB, but let's you understand the steps...

<一个href=\"http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider\">http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider

我不只是创造我自己的会员提供者,但我创建了角色提供商以及,巫婆你可以从上面code看到的,是因为成员身份简单,让你,在你的应用程序中使用的东西,如:

I did not only create my own Membership Provider but I created my Roles Provider as well, witch as you can see from above code, is as simple as the MemberShip and let's you, in your application use things like:

[Authorize(Roles = "Partner, Admin")]
public ActionResult MyAction()
{

}

@if (Roles.IsUserInRole(Context.User.Identity.Name, "Admin"))
{
    <div>You're an ADMIN, Congrats!</div>
}


什么是自动创建所需的方法(周期+空间,在继承类)

您可以用鼠标右键单击,或对名称和$ P $光标PSS <大骨节病>控制 + <大骨节病>。然后<大骨节病>空格

这篇关于在.NET 4.0中自定义的MembershipProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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