C#.NET 安全操作类

 

using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;

namespace Pub.Class {
/// <summary>
/// 安全操作类
/// </summary>
public class Safe {

#region IsSafeUrl
/// <summary>
/// 不允许在本地提交数据
/// </summary>
/// <remarks>返回是否是安全URL</remarks>
/// <param name="doMain">域名</param>
public static bool IsSafeUrl(string doMain) {
string url = Request2.GetReferrer().ToLower().Trim().Replace("http://", "").Replace("https://", "").Split('/')[0];
doMain
= doMain.ToLower().Trim();
if (url.IndexOf(doMain)>-1) return true;
return false;
}
#endregion

#region Kill/Run
/// <summary>
/// 杀死进程
/// </summary>
/// <param name="processName">进程名</param>
public static void Kill(string processName)
{
System.Diagnostics.Process myproc
= new System.Diagnostics.Process();
System.Diagnostics.Process[] procs
= System.Diagnostics.Process.GetProcessesByName(processName);// '得到所有打开的进程
try {
foreach (System.Diagnostics.Process proc in procs) {
if (!proc.CloseMainWindow()) { proc.Kill(); }
}
}
catch { } finally { myproc.Dispose(); }
}
/// <summary>
/// 运行一个进程
/// </summary>
/// <param name="cmd"></param>
/// <param name="arguments"></param>
/// <param name="winStyle"></param>
/// <returns></returns>
public static bool Run(string cmd,string arguments,System.Diagnostics.ProcessWindowStyle winStyle) {
bool _isTrue = false;
if (cmd.Trim().Equals("")) return false;
try {
System.Diagnostics.Process pScore
= new System.Diagnostics.Process();
pScore.StartInfo.FileName
= cmd;
pScore.StartInfo.Arguments
= arguments;
pScore.StartInfo.ErrorDialog
= false;
pScore.StartInfo.UseShellExecute
= true;
pScore.StartInfo.RedirectStandardOutput
= false;
pScore.StartInfo.WindowStyle
= winStyle;
pScore.Start();
pScore.Close();
_isTrue
= true;
}
catch { }
return _isTrue;
}
#endregion

#region 防刷新页面代码
/// <summary>
/// 设置打开页面的时间
/// </summary>
public static void SetDateTime() {
Cookie2.Set(
"__sysTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
/// <summary>
/// 判断是否在指定多少秒内提交数据,来达到判断是否刷新页面的目的
/// </summary>
/// <param name="seconds">多少秒内</param>
/// <returns>是/否</returns>
public static bool IsRefresh(int seconds) {
string _sysTime = Cookie2.Get("__sysTime");
if (_sysTime.Trim()=="") return true;
if (!_sysTime.IsDateTime()) return true;
DateTime _startTime
= DateTime.Parse(_sysTime);
DateTime _endTime
= DateTime.Now;
TimeSpan _value
= _startTime.GetTimeSpan(_endTime);
if (_value.Seconds >= seconds) return false;
else {
Js.Alert(
"不允许刷新,或快速提交数据,请" + seconds.ToString() + "秒后提交数据。");
return true;
}
}
#endregion

public static void SafeGetPost(string doMain){
if (string.IsNullOrEmpty(doMain)) return;
bool isTrue = false;
string[] doMainArr = doMain.Split('|');
for (int i = 0; i <= doMainArr.Length - 1; i++) if (Safe.IsSafeUrl(doMainArr[i])) isTrue = true;
if (!isTrue) { Msg.Write("不允许在本地提交数据。"); Msg.End(); }
}

public static object DllInvoke(string DllFileName, string NameSpace, string ClassName, string MethodName, object[] ObjArrayParams){
Assembly DllAssembly
= Assembly.LoadFrom(DllFileName);
Type[] DllTypes
= DllAssembly.GetTypes();
foreach (Type DllType in DllTypes){
if (DllType.Namespace == NameSpace && DllType.Name == ClassName){
MethodInfo MyMethod
= DllType.GetMethod(MethodName);
if (MyMethod != null){
object mObject = Activator.CreateInstance(DllType);
return MyMethod.Invoke(mObject, ObjArrayParams);
}
}
}
return (object)0;
}

}
}

 

posted @ 2010-07-05 21:56  熊哥  阅读(582)  评论(1编辑  收藏  举报