我需要为我的Web应用程序运行身份验证,以便不同的授权用户可以访问指定的信息。我遵循Introduction to Identity on ASP.NET Core(MSDN)作为为我的用户激活身份验证,它看起来没问题。然后,我跟着Create an ASP.NET Core app with user data protected by authorization(MSDN)进行授权,但我无法继续。ASP.Net Web应用程序中的身份验证和授权问题
当我运行这个程序......
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvcWithDefaultRoute();
// Set password with the Secret Manager tool.
// dotnet user-secrets set SeedUserPW <pw>
var testUserPw = Configuration["SeedUserPW"];
if (String.IsNullOrEmpty(testUserPw))
{
throw new System.Exception("Use secrets manager to set SeedUserPW \n" +
"dotnet user-secrets set SeedUserPW <pw>");
}
try
{
SeedData.Initialize(app.ApplicationServices, testUserPw).Wait();
}
catch
{
throw new System.Exception(@"You need to update the DB
\nPM > Update-Database \n or \n
> dotnet ef database update
\nIf that doesn't work, comment out SeedData and
register a new user");
}
...我得到这个错误:
System.Exception: 'You need to update the DB PM > Update-Database or > dotnet ef database update If that doesn't work, comment out SeedData and register a new user'
我更新了数据库,并获得成功更新,但错误依然存在。我也改变了用户和密码,但没有发生任何事情。
如何在我的Web应用程序上激活身份验证和授权?
向我们展示您的代码并告诉我们关于您自己实现的异常的提示并没有什么帮助。相反,您是否可以用下面的代码替换所有* try *'catch'块代码:'SeedData.Initialize(app.ApplicationServices,testUserPw).Wait();',运行你的程序并告诉我们哪个错误得到了抛出? –
嗨@QualityCatalyst,我追了这个问题,我得到了波纹异常: 'ArgumentNullException:值不能为空.'当程序读取seedData calss,方法“EnsureRole”时抛出异常。 'var user = await userManager.FindByIdAsync(uid);'因为用户为空。 –