lak*_*k-b 3 asp.net authentication routing
现在我正在尝试使用System.Web.Routing.一切都很好,但我无法理解如何使用url路由进行表单身份验证(返回URL,重定向等).谷歌没有说什么.救命!:)
UPD:我忘了 - 我不使用MVC.那就是问题所在.如何在没有MVC的情况下使用rounig和表单身份验证
UPD2:更多关于我的问题
我想得到的:使用Routes的URL" mysite.com/content/123"," mysite.com/login/"等.使登录页面像"常规"ASP.NET登录表单一样重要(在不登录时重定向到从安全区域登录,并在登录时重定向回安全区域).
这就是我正在做的事情.
在global.asaxon Application_Start,注册这样的路线:
routes.Add("LoginPageRoute", new Route("login/", new CustomRouteHandler("~/login.aspx")));
routes.Add("ContentRoute", new Route("content/{id}", new ContentRoute("~/content.aspx"))
{
Constraints = new RouteValueDictionary {{ "id", @"\d+" }}
});
Run Code Online (Sandbox Code Playgroud)
在哪里CustomRouteHandler和ContentRoute- 简单的IRouteHandler类,就像:...
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
Run Code Online (Sandbox Code Playgroud)
...
一切似乎都很完美:我content.aspx什么时候去“/content/10”,login.aspx什么时候去“/login/”.但是......
当我确保内容安全(in web.config,with deny=”?”)时,登录表单不能像预期的那样工作.
现在我无法访问该“/content/10”页面:
0.我“/content/10”在浏览器中输入内容.
1.网站重定向到“/login/?ReturnUrl=%2fcontent%2f10”.(嗯......似乎所有的问题都从这里开始,对吧?:)
2.我正在尝试登录.无论我输入的凭据是什么......
3. ...网站将我重定向到“login?ReturnUrl=%2fContent%2f10”(错误的黄色屏幕 - Access is denied.说明:An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL.)
因此,问题是如何让ASP.NET了解真实ReturnUrl并在登录后提供重定向.
这些步骤应该允许您实现所需的行为.
总结一下:
我使用Pages文件夹中的web.config"隐藏"了Pages文件夹的内容:
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<pages validateRequest="false">
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这可以确保如果有人使用http://host/Mysite/Pages/users.aspx?userid = 12345这样的网址,那么他们会收到标准的404响应.
我的顶级web.config文件包含(以及所有标准内容)此location元素:
<location path="userid">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
这可以防止匿名访问http:// host/Mysite/userid/12345格式的网址,这意味着用户将自动重定向到login.aspx,然后如果他们提供有效的凭据,他们将被重定向到正确的位置.
这里参考的是我的global.asax:
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.Add("UseridRoute", new Route
(
"userid/{userid}",
new CustomRouteHandler("~/Pages/users.aspx")
));
}
</script>
Run Code Online (Sandbox Code Playgroud)
这是我的路线处理程序:
using System.Web.Compilation;
using System.Web.UI;
using System.Web;
using System.Web.Routing;
using System.Security;
using System.Web.Security;
public interface IRoutablePage
{
RequestContext RequestContext { set; }
}
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext
requestContext)
{
var page = BuildManager.CreateInstanceFromVirtualPath
(VirtualPath, typeof(Page)) as IHttpHandler;
if (page != null)
{
var routablePage = page as IRoutablePage;
if (routablePage != null) routablePage.RequestContext = requestContext;
}
return page;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2819 次 |
| 最近记录: |