我有以下web.config文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="canView"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
我遇到的问题是有时用户正确担任该角色 (并且出于测试目的,这实际上是所有用户)在点击指向受保护页面的链接时被定向到登录页面。
我在主页面上包含了调试语句,以验证它们是否经过身份验证,并确实具有以下所需的角色:
if (!userMgr.IsInRole(userMgr.FindByName(HttpContext.Current.User.Identity.Name).Id, RoleActions.ROLE_CANVIEW))
{
DoError(String.Format("User NOT in role - Name={0}, Authenticated={1}", Context.User.Identity.Name, Context.User.Identity.IsAuthenticated));
}
else
{
DoError(String.Format("User IS IN in role - Name={0}, Authenticated={1}", Context.User.Identity.Name, Context.User.Identity.IsAuthenticated));
}
我还可以验证用户是否已通过身份验证,因为在母版页中使用了LoggedInTemplate。
这个问题并不是一直发生,但往往令人担忧。此外,当它确实发生时 - 它仍然存在:你可以关闭网页,浏览器,重新打开,问题仍然存在,直到不知怎的消失。
对不起,如果这一切看起来都模糊不清 - 我是ASP.NET(Windows开发人员)的新手,也是微软的WingTip示例项目的工作人员,但我不理解这种不一致的行为。
有人可以帮忙吗?
如果有帮助,我正在使用OWIN 2.1.0.0和自定义身份验证。
这是我的App_Start代码
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301883
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Use a cookie to temporarily store information about a user logging in with a third party login provider
// SJW
//app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromSeconds(3),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
和登录代码
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
// This doen't count login failures towards account lockout
// To enable password failures to trigger lockout, change to shouldLockout: true
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, false, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
FormsAuthentication.SetAuthCookie(Email.Text, false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
case SignInStatus.LockedOut:
FailureText.Text = "Locked out";
ErrorMessage.Visible = true;
break;
case SignInStatus.Failure:
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
break;
default:
FailureText.Text = "Invalid login attempt";
ErrorMessage.Visible = true;
break;
}
}
}