如何初始化ASP.NET MVC4中的简单成员资格
嗨,我正在研究ASP.NET MVC4。 如您所知,ASP.NET MVC4中禁用了成员身份/身份验证机制; 如果我们想要启用它,我们需要:
1)编辑跟随行(在SimpleMembershipInitializer类的构造函数中,文件InitializeSimpleMembershipAttribute.cs)
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "users", "id", "username", autoCreateTables: true);
2)在Web.Config部分添加一个新密钥。
<appSettings>
<add key="enableSimpleMembership" value="true" />
...
</appSettings>
NB:我已经在我的数据库中创建了表'users',DefaultConnection设置正确。
编辑:让“DefaultConnectionString”为
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-SimpleAuthenticationMvc4-20140209174604;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
当我尝试在我的网络应用程序中“登录”时,在调用EnsureInitialized()时出现TargetInvocationException: 这里是我的InitializeSimpleMembershipAttribute类的完整源代码:
/* class MyApp.Filters.InitializeSimpleMembershipAttribute */
namespace MyApp.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
// HERE i _always_ get TargetInvocationException
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
Database.SetInitializer<UsersContext>(null);
try
{
using (var context = new UsersContext())
{
if (!context.Database.Exists())
{
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
/* DefaultConnection and table 'users' should/seems to be OK */
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "users", "id", "username", autoCreateTables: true);
}
catch (Exception ex)
{
...
}
}
}
}
}
有人建议我这个错误可能意味着简单的成员资格在应用程序的另一部分初始化,但我不明白这可能发生在哪里: 这是我的global.asax.cs文本:
/* file global.asax.cs */
namespace MyApp
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
}
正如你所看到的,没什么奇怪的。 我也尝试在文件global.asax.cs中移动,如此处所示,但即便如此,我也得到相同的异常。
我没有特殊要求:我只想登录我的MVC 4应用程序,就像我之前写过的MVC3应用程序一样。
有谁能告诉我哪里出错了?
谢谢!
IT
答案 0 :(得分:0)
好的,我假设您要使用现有用户表吗? “DefaultConnection”是您的连接字符串吗?我假设没有。所以你应该使用你的连接字符串。
最初我也遇到了初始化问题,所以我倾向于抛弃过滤器并直接在global.asax中初始化它
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("YOUR_DB_CONTEXT", "USER_TABLE", "ID_COLUMN", "USERNAME_COLUMN", true);
}
我发布了关于设置它的教程Here。我认为它可以帮助你。