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

查看:39
本文介绍了.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 服务器 (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.

因此,如果有人获得了一些展示如何操作的资源,或者愿意在此处发布代码示例或类似内容,我将不胜感激.

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 异常,您需要做的就是编辑每个方法并放置您自己的代码.例如,我定义了 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);
}

db 是我作为

MyServicesRepository db = new MyServicesRepository();

在那里,您会发现 GetUser 方法为:

there, you will find the GetUser method as:

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、<代码>删除用户等

Do this for all the methods you use (debug the project and see which ones you need) - I only use some, not all as I don't really care about methods like ChangePasswordQuestionAndAnswer, DeleteUser, etc

只需确保在您的 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>

<小时>

你有一个很好的 视频教程 来自 Chris Pels(日期为 2007 但仍然大部分有效)和代码,虽然视频教程是在 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...

http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider

我不仅创建了自己的 Membership Provider,而且 我创建了我的 Roles Provider,正如您从上面的代码中看到的那样,就像 MemberShip 一样简单,让您在您的应用程序中使用以下内容:

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>
}

<小时>

什么是自动创建所需的方法(继承类中的句点+空格)

您可以右键单击,或将光标放在名称上,然后按 Control + .,然后按 space.

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

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