Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何覆盖某些网页的身份验证。。。。?_C#_.net_Asp.net_Authentication_Forms Authentication - Fatal编程技术网

C# 如何覆盖某些网页的身份验证。。。。?

C# 如何覆盖某些网页的身份验证。。。。?,c#,.net,asp.net,authentication,forms-authentication,C#,.net,Asp.net,Authentication,Forms Authentication,我正在母版页中编写以下代码:- <authentication mode="Forms"> <forms loginUrl="Loginpage.aspx" /> </authentication> 现在,如果身份验证失败,它将重定向到“Loginpage.aspx” 现在,如果我想覆盖这几个页面的身份验证,该怎么办。还要注意,页面数和页面名称在设计时不可用,因此不能在配置文件中包含aspx页面名称 是否存在覆盖少数aspx页面的身份验证的方法

我正在母版页中编写以下代码:-

<authentication mode="Forms">
    <forms loginUrl="Loginpage.aspx" />
</authentication>

现在,如果身份验证失败,它将重定向到“Loginpage.aspx”

现在,如果我想覆盖这几个页面的身份验证,该怎么办。还要注意,页面数和页面名称在设计时不可用,因此不能在配置文件中包含aspx页面名称

是否存在覆盖少数aspx页面的身份验证的方法


-Anil

您可以监听AuthorizeRequest事件并相应地采取行动。创建您自己的Http模块来执行此操作

三种选择:

  • 将上述配置设置与生成包含web.config条目的文件夹一起使用。这是一种相当拙劣的做法

  • 听一下事件AuthenticateRequest,代码如下所示:

    public class UserAuthenticationModule : IHttpModule
    {
    private HttpApplication _Context;
    private RoleManagerModule _RoleManager;
    
    public void Init(HttpApplication context)
    {
        _Context = context;
        context.AuthenticateRequest += AuthenticateUser;
        _RoleManager = (RoleManagerModule)context.Modules["RoleManager"];
        _RoleManager.GetRoles += roleManager_GetRoles;
    }
    
    // http://stackoverflow.com/questions/1727960/how-to-keep-roleprovider-from-overriding-custom-roles
    private void roleManager_GetRoles(object sender, RoleManagerEventArgs e)
    {
        if (_Context.User is UserPrincipal)
            e.RolesPopulated = true; // allows roles set in AuthenticateUser to stick.
    }
    
    private static void AuthenticateUser(object sender, EventArgs e)
    {
        var app = (HttpApplication) sender;
        if (app.Context == null) return;
    
        var user = app.Context.User;
    
        // not signed in, forms authentication module takes care of redirecting etc.
        if (user == null) return;
        // we're done then.
        if (user is IUser) return;
    
        var userEntity = IoC.Resolve<IUserRepository>().FindByUserName(user.Identity.Name);
    
        // we can't find the user in the database.
        if (userEntity == null)
            throw new ApplicationException(string.Format("User \"{0}\" deleted from, or renamed in, database while logged into application.", 
                user.Identity.Name));
    
        // signed in, assigning user, which should assign Thread.CurrentPrincipal as well (it wouldn't do this on PostAuthenticateRequest).
        app.Context.User = new UserPrincipal(userEntity);
        userEntity.SetAuthenticated();
    }
    
    //Implement IDisposable.
    public void Dispose()
    {
    }
    }
    
    公共类UserAuthenticationModule:IHttpModule { 私有HttpApplication_上下文; 专用角色管理器模块_角色管理器; 公共void Init(HttpApplication上下文) { _上下文=上下文; context.AuthenticateRequest+=AuthenticateUser; _RoleManager=(RoleManager模块)context.Modules[“RoleManager”]; _RoleManager.GetRoles+=RoleManager\u GetRoles; } // http://stackoverflow.com/questions/1727960/how-to-keep-roleprovider-from-overriding-custom-roles 私有无效roleManager_GetRoles(对象发送方,roleManager事件参数e) { 如果(_Context.User是UserPrincipal) e、 RolesPopulated=true;//允许AuthenticateUser中设置的角色保持不变。 } 私有静态无效身份验证器(对象发送方,事件参数e) { var-app=(HttpApplication)发送方; if(app.Context==null)返回; var user=app.Context.user; //未登录,表单身份验证模块负责重定向等。 if(user==null)返回; //那我们就完了。 如果(用户是IUser)返回; var userEntity=IoC.Resolve().FindByUserName(user.Identity.Name); //我们在数据库中找不到用户。 if(userEntity==null) 抛出新的ApplicationException(string.Format(“用户\“{0}\”在登录到应用程序时从数据库中删除或重命名), user.Identity.Name)); //已登录,正在分配用户,该用户还应分配Thread.CurrentPrincipal(在PostAuthenticateRequest上不会这样做)。 app.Context.User=新用户主体(userEntity); userEntity.SetAuthenticated(); } //实现IDisposable。 公共空间处置() { } }
如果您的UserPrincipal实现了IPrincipal,那么IsInRole将用于为您的页面提供基于角色的访问权限

  • 面向服务的方式;设置一个小型透明代理服务器。在动态存储中列出您的端点/uri-s,如您所描述的。设置授权服务,如Rhino Security;将其服务接口公开为REST-API或请求/应答接口等。从web应用程序的角度假设每个请求都是允许的,并注意重定向的位置。在代理服务器中,例如nginx,它是Linux上一个非常好的基于C的异步代理服务器,可以通过过滤器/模块调用您的授权服务“深度安全”,您可以在授权服务中共享安全配置


您遵循的原则是,如果web应用程序中不允许某些内容,则在AuthorizeRequest事件中抛出新的HttpException(405,“您尝试执行的当前操作现在允许您的角色或用户或生命中选择的路径”)注意,有一个AuthenticateRequest和另一个AuthorizeRequest事件

您可以监听AuthorizeRequest事件并相应地采取行动。创建您自己的Http模块来执行此操作

三种选择:

  • 将上述配置设置与生成包含web.config条目的文件夹一起使用。这是一种相当拙劣的做法

  • 听一下事件AuthenticateRequest,代码如下所示:

    public class UserAuthenticationModule : IHttpModule
    {
    private HttpApplication _Context;
    private RoleManagerModule _RoleManager;
    
    public void Init(HttpApplication context)
    {
        _Context = context;
        context.AuthenticateRequest += AuthenticateUser;
        _RoleManager = (RoleManagerModule)context.Modules["RoleManager"];
        _RoleManager.GetRoles += roleManager_GetRoles;
    }
    
    // http://stackoverflow.com/questions/1727960/how-to-keep-roleprovider-from-overriding-custom-roles
    private void roleManager_GetRoles(object sender, RoleManagerEventArgs e)
    {
        if (_Context.User is UserPrincipal)
            e.RolesPopulated = true; // allows roles set in AuthenticateUser to stick.
    }
    
    private static void AuthenticateUser(object sender, EventArgs e)
    {
        var app = (HttpApplication) sender;
        if (app.Context == null) return;
    
        var user = app.Context.User;
    
        // not signed in, forms authentication module takes care of redirecting etc.
        if (user == null) return;
        // we're done then.
        if (user is IUser) return;
    
        var userEntity = IoC.Resolve<IUserRepository>().FindByUserName(user.Identity.Name);
    
        // we can't find the user in the database.
        if (userEntity == null)
            throw new ApplicationException(string.Format("User \"{0}\" deleted from, or renamed in, database while logged into application.", 
                user.Identity.Name));
    
        // signed in, assigning user, which should assign Thread.CurrentPrincipal as well (it wouldn't do this on PostAuthenticateRequest).
        app.Context.User = new UserPrincipal(userEntity);
        userEntity.SetAuthenticated();
    }
    
    //Implement IDisposable.
    public void Dispose()
    {
    }
    }
    
    公共类UserAuthenticationModule:IHttpModule { 私有HttpApplication_上下文; 专用角色管理器模块_角色管理器; 公共void Init(HttpApplication上下文) { _上下文=上下文; context.AuthenticateRequest+=AuthenticateUser; _RoleManager=(RoleManager模块)context.Modules[“RoleManager”]; _RoleManager.GetRoles+=RoleManager\u GetRoles; } // http://stackoverflow.com/questions/1727960/how-to-keep-roleprovider-from-overriding-custom-roles 私有无效roleManager_GetRoles(对象发送方,roleManager事件参数e) { 如果(_Context.User是UserPrincipal) e、 RolesPopulated=true;//允许AuthenticateUser中设置的角色保持不变。 } 私有静态无效身份验证器(对象发送方,事件参数e) { var-app=(HttpApplication)发送方; if(app.Context==null)返回; var user=app.Context.user; //未登录,表单身份验证模块负责重定向等。 if(user==null)返回; //那我们就完了。 如果(用户是IUser)返回; var userEntity=IoC.Resolve().FindByUserName(user.Identity.Name); //我们在数据库中找不到用户。 if(userEntity==null) 抛出新的ApplicationException(string.Format(“用户\“{0}\”在登录到应用程序时从数据库中删除或重命名), user.Identity.Name)); //已登录,正在分配用户,该用户还应分配Thread.CurrentPrincipal(在PostAuthenticateRequest上不会这样做)。 app.Context.User=新用户主体(userEntity); userEntity.SetAuthenticated(); } //实现IDisposable。 公共空间处置() { } }
如果您的UserPrincipal实现了IPrincipal,那么IsInRole将用于为您的页面提供基于角色的访问权限

  • 面向服务的方式;设置一个小型透明代理服务器。在动态存储中列出您的端点/uri-s,如您所描述的。设立授权机构