您将需要实现自己的成员资格和角色提供程序。
自定义提供者:
namespace MyApp.Helpers
{
public class CustomProviderProvider : SimpleMembershipProvider
{
public override MembershipUser GetUser(string username, bool userIsOnline)
{
return base.GetUser(username, userIsOnline);
}
public override bool ValidateUser(string username, string password)
{
return true; // base.ValidateUser(username, password);
}
}
}
您只需要覆盖您想要“拦截”的方法(即ValidateUser)。您还需要在 web.config 文件中注册提供程序:
<system.web>
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
<add name="CustomMembershipProvider" type="MyApp.Helpers.CustomMembershipProvider, MyApp" />
</providers>
</membership>
</system.web>
您还应该有一个名为“InitializeSimpleMembershipAttribute”的过滤器
namespace CustomPortal.Filters
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
Database.SetInitializer<CustomPortalContext>(null);
try
{
using (var context = new CustomPortalContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
//Here is where you give it your connection string name
WebSecurity.InitializeDatabaseConnection("CustomPortal", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true);
}
catch (Exception ex)
{
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
}
我希望这有帮助...