当我们选择New Project --> MVC 4 --> Internet Application
时,它会自动为我们生成AccountController。在此控制器中,我只关心2个操作,Login
和Register
。
在MVC 3中,它使用了成员资格的静态方法,ValidateUser
操作中的Login
和CreateUser
中的Register
。所以,如果我想将它与我自己的数据库集成,我只需要通过扩展CustomMembershipProvider
来创建MembershipProvider
并覆盖这两种方法。
但在MVC 4中,它使用WebSecurity.Login
和WebSecurity.CreateUserAndAccount
。我的问题是:
WebSecurity
?非常感谢你的帮助。
答案 0 :(得分:17)
您需要实现自己的会员和角色提供者。
自定义提供商:
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);
}
}
}
}
}
我希望这会有所帮助......
答案 1 :(得分:2)
ASP.NET MVC 4使用名为SimpleMembershipProvider的新成员资格提供程序。 WebMatrix是SimpleMembershipProvider的外观。不再需要使用ASP.NET MVC 4修改配置文件。更多信息请访问:http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html
1.如何使用我自己的数据库,就像在MVC 3中一样?
答:如果你想自己动手,不需要覆盖SimpleMembershipProvider。删除InitializeSimpleMembershipAttribute
类中AccountController
的引用,并修改AccountController
类中的方法。
2.有什么不同?为什么不继续使用静态成员资格方法?为什么必须更改为WebSecurity?
答:ASP.NET MVC 4现在使用新的成员资格提供程序:SimpleMembershipProvider。