模拟登陆os

IIS模拟登录

 

由于IIS的服务帐户往往没有权限,IO操作比较容易出问题,采用模拟登录

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Security.Principal;
using System.Runtime.InteropServices;


using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    
public const int LOGON32_LOGON_INTERACTIVE = 2;
    
public const int LOGON32_PROVIDER_DEFAULT = 0;
    WindowsImpersonationContext impersonationContext;
    [DllImport(
"advapi32.dll")]
    
public static extern int LogonUserA(String lpszUserName,
     String lpszDomain,
     String lpszPassword,
     
int dwLogonType,
     
int dwLogonProvider,
     
ref IntPtr phToken);
    [DllImport(
"advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    
public static extern int DuplicateToken(IntPtr hToken,
     
int impersonationLevel,
     
ref IntPtr hNewToken);

    [DllImport(
"advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    
public static extern bool RevertToSelf();
    [DllImport(
"kernel32.dll", CharSet = CharSet.Auto)]
    
public static extern bool CloseHandle(IntPtr handle);


    
protected void Page_Load(object sender, EventArgs e)
    
{
        

    }

    
protected void button1_Click(object sender, EventArgs e)
    
{
        
if (impersonateValidUser("test1""contoso.local""password01!"))
        
{
            
//Insert your code that runs under the security context of a specific user here.
            
//button1.Text = "ok";

            
byte[] att = File.ReadAllBytes(@"/fileserver est1 est.txt");
            
string contentType = "application/octet-stream";
            
string filename = "test.txt";

            Response.AppendHeader(
"Content-Disposition""attachment;FileName=" + filename);
            Response.AppendHeader(
"Content-Length", att.Length.ToString());

            Response.BinaryWrite(att);
            Response.Flush();

            undoImpersonation();
        }

        
else
        
{
            
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
        }

    }



    
private bool impersonateValidUser(String userName, String domain, String password)
    
{
        WindowsIdentity tempWindowsIdentity;
        IntPtr token 
= IntPtr.Zero;
        IntPtr tokenDuplicate 
= IntPtr.Zero;
        
if (RevertToSelf())
        
{
            
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
             LOGON32_PROVIDER_DEFAULT, 
ref token) != 0)
            
{
                
if (DuplicateToken(token, 2ref tokenDuplicate) != 0)
                
{
                    tempWindowsIdentity 
= new WindowsIdentity(tokenDuplicate);
                    impersonationContext 
= tempWindowsIdentity.Impersonate();
                    
if (impersonationContext != null)
                    
{
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        
return true;
                    }

                }

            }

        }

        
if (token != IntPtr.Zero)
            CloseHandle(token);
        
if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        
return false;
    }

    
private void undoImpersonation()
    
{
        impersonationContext.Undo();
    }


}

 

 

### 关于鸿蒙OS页面的ETS实现 在构建基于鸿蒙操作系统的应用程序时,尤其是涉及用户交互的部分(如登页面),开发者通常会利用ETS(Efficient TypeScript)作为主要编程语言之一。以下是有关如何通过ETS实现鸿蒙OS页面的设计与教程。 #### 使用ETS设计登页面的核心概念 为了创建一个功能完善的登页面,在开发过程中需注意以下几点: 1. **选择合适的文件类型** 当创建一个新的页面用于展示登界面时,应确保该页面是一个独立的`Page`而非组件形式。这是因为只有`Page`类型的文件才能被设置为应用的入口文件并直接渲染到屏幕上[^2]。 2. **布局与样式定义** 利用ArkUI框架中的各种控件来搭建登表单结构,比如输入框(`TextField`)、按钮(Button)等基本元素,并为其绑定相应的事件处理函数以便响应用户的点击动作或其他行为。 3. **数据验证逻辑编写** 对用户提交的数据进行必要的校验是非常重要的一步。可以通过监听特定字段的变化或者拦截整个表单提交过程来进行实时反馈提示错误消息等功能。 4. **导航控制配置** 完成身份认证之后可能还需要跳转至其他业务模块,则可以借助Router API完成不同场景之间的切换过渡效果设定等工作。 #### 示例代码片段 下面提供了一个简单的示例程序展示了上述提到的一些要点: ```typescript // mainAbility/pages/Login/index.ets @Entry @Component struct LoginPage { @State username: string = ''; @State password: string = ''; build() { Column({ space: 8 }) { Text('用户名') .fontSize(20) TextField(this.username) .onChange((value) => this.username = value) Text('密码') .fontSize(20) TextField(this.password, { type: InputType.Password }) .onChange((value) => this.password = value) Button('登', () => { console.log(`尝试使用 ${this.username} 和 密码登`); // TODO 添加实际的身份验证流程调用接口请求等内容... }).padding(16).backgroundColor(Color.Blue).color(Color.White) }.width('90%').alignItems(HorizontalAlign.Center) } } ``` 此段脚本定义了一个基础版的登陆视图,其中包含了两个文本域分别接收使用者名以及密语字符串;还有一个按键负责触发模拟签入的操作记输出调试信息而已——真正的应用场景下应当替换成为正式的服务端通信部分。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值