mim*_*mic 21 asp.net authentication authorization
我需要将我的用户重定向到AuthError.aspx页面("您无权访问此页面"),如果他们经过身份验证但尝试访问他们无法访问的页面(因为考试的角色) .如果我设置了web.config,那么:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)
这是系统的错误行为,因为用户已经过身份验证,无需将他或她重定向到此页面.但是,如果我在这里写AuthError.aspx而不是Login.aspx我怎么能将尚未验证的用户重定向到登录页面?
Joe*_*ham 24
在登录页面的Page_Load上,您需要检查用户是否已通过身份验证,以及是否要将其重定向到拒绝访问的页面:
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated) // if the user is already logged in
{
Response.Redirect("~/AccessDenied.aspx");
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想获得一点点发烧友,你可以查看ReturnUrl参数来确定用户是否直接进入页面(例如通过他们保存到登录页面的书签)并以不同的方式处理.这是一个例子:
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
// if they came to the page directly, ReturnUrl will be null.
if (String.IsNullOrEmpty(Request["ReturnUrl"]))
{
/* in that case, instead of redirecting, I hide the login
controls and instead display a message saying that are
already logged in. */
}
else
{
Response.Redirect("~/AccessDenied.aspx");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你需要:
1) 启用角色(在 web.config 中):(将 'xxx' 替换为您自己的值)
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="xxx"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="xxx" name="AspNetWindowsTokenRoleProvider"
type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
Run Code Online (Sandbox Code Playgroud)
2) 您需要限制特定角色对网站某些区域的访问。我今天实际上回答了另一个问题,其中解释了如何实现这一目标。 链接在这里
| 归档时间: |
|
| 查看次数: |
62001 次 |
| 最近记录: |