您需要实现自己的会员身份验证和角色提供程序。
自定义提供程序:
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;
}
}
}
你只需要重写你想要“拦截”的方法(例如 ValidateUser)。你还需要在 web.config 文件中注册该提供程序:
你只需要覆盖想要“拦截”的方法(如 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)
{
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())
{
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
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);
}
}
}
}
}
我希望这能帮到你...