我使用VS2017创建了一个ASP.NET Web Forms模板项目。
我正在尝试使表单身份验证工作。目前我无法获得任何重定向到我的登录页面。行为是:
浏览网站=>拒绝访问。没有看到登录页面。此外,如果我尝试浏览localhost / Account / Login的登录页面,则表示拒绝访问。
在Web.config中,我添加了表单身份验证部分并指定了登录页面:
<appSettings>
<add key="owin:AutomaticAppStartup" value="false"/>
</appSettings>
<system.web>
<trust level="Full" />
<authentication mode="Forms">
<forms name="dealer-bdopcycling" path="/" defaultUrl="~/" loginUrl="~/Account/Login" protection="All" timeout="30" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>...
此外,正如您所看到的,我阻止了所有未经身份验证的用户的访问权限。
此外,由于模板和this帖子之后的无限重定向循环,我添加了关键来阻止Owin自动应用启动。
我已确认设置:匿名身份验证已启用,Windows身份验证已禁用。
另一个奇怪的行为是,如果我删除了密钥以停止Owin自动启动,如果我浏览到应用程序(或登录页面)我没有获得访问被拒绝我获得了重定向,但是无限重定向循环和服务器错误。
模板附带的Startup.Auth.cs中的代码在另一篇帖子中说是造成部分麻烦的,我还发现注释掉登录路径行意味着无限循环消失且无法访问任何回报:
public partial class Startup {
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"), // commenting out this changes infinite loop to cannot login
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
我想知道如何修复它?
答案 0 :(得分:1)
您拒绝所有用户<deny users="?"/>
访问整个网站。这包括登录页面。这就是你得到一个循环的原因。因此,您需要授予所有用户查看登录页面的权限。
在</system.web>
<location path="~/Account/Login.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
虽然无法帮助您使用owin
部分。没有经验。
答案 1 :(得分:1)
默认模板在Web.config中包含以下行,这是我的问题的原因(被注释掉的那一行):
<system.webServer>
<modules>
<!--<remove name="FormsAuthentication" />-->
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>