gpt4 book ai didi

c# - 你必须调用 "WebSecurity.InitializeDatabaseConnection"但我知道

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:25 27 4
gpt4 key购买 nike

*You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.*

我收到这个错误,时断时续,随机出现,不确定是什么原因。在我的路线配置中。索引 Controller ,索引操作是默认的。这是定义:

[Authorize(Roles = "admin")]
[InitializeSimpleMembership]
public class IndexController : Controller

每次我重新启动应用程序时,如果我没有注销,我就会收到上述错误。然后我注销并再次登录,错误消失了。

为什么会这样?

我该如何解决这个问题?

如果有帮助,我正在使用 localdb。

最佳答案

出现此问题是因为 Application_Start() 函数仅在第一个用户操作时触发。但是 IndexController 在用户可以事件执行操作之前被触发。只需从您的项目中删除 [InitializeSimpleMembership],这也让我很沮丧......

只需将以下代码添加到您的 global.asax

private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();

LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}

public class SimpleMembershipInitializer
{
public SimpleMembershipInitializer()
{
using (var context = new UsersContext())
context.UserProfiles.Find(1);

if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
}

当然,请确保您创建了正确的 dbContext() 并设置了与您的项目相匹配的正确的 InitializeDatabaseConnections 属性。

关于c# - 你必须调用 "WebSecurity.InitializeDatabaseConnection"但我知道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17809782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com