📄 service.cs
字号:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Cryptography;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Xml;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
private string rootdir = ConfigurationManager.AppSettings["UploadDisk"].ToString();
private string UploadPath;
public Service () {
}
#region Validate
/// <summary>
/// 验证用户的合法性
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <returns>合法与否</returns>
private bool ValidUser(string username, string password)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
conn.Open();
SqlCommand cmd = new SqlCommand("select id,name from users where name=@_name and pass=@_pass", conn);
cmd.Parameters.Add("@_name", SqlDbType.VarChar);
cmd.Parameters.Add("@_pass", SqlDbType.VarChar);
cmd.Parameters[0].Value = username;
cmd.Parameters[1].Value = password;
SqlDataReader dr = cmd.ExecuteReader();
cmd.Dispose();
if (dr.Read())
{
dr.Close();
conn.Close();
return true;
}
else
{
dr.Close();
conn.Close();
return false;
}
}
[WebMethod(Description = "保证网络畅通")]
public void Ping()
{
}
/// <summary>
/// 获取票据
/// </summary>
/// <param name="UserName">用户名</param>
/// <param name="PassWord">密码</param>
/// <returns>验证合法后返回加密票据,否则返回空</returns>
[WebMethod(Description="获取票据")]
public string GetAuthorizationTicket(string UserName, string PassWord)
{
if (ValidUser(UserName, PassWord))
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(UserName, false, 1);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
int timeout = 20;
Context.Cache.Insert(encryptedTicket, UserName, null, DateTime.Now.AddMinutes(timeout), TimeSpan.Zero);
return encryptedTicket;
}
else
{
return "失败";
}
}
/// <summary>
/// 验证加密票据是否有效
/// </summary>
/// <param name="ticket">加密票据</param>
/// <returns>有效与否</returns>
private bool IsTicketValid(string ticket,bool IsAdminCall)
{
///查看加密票据是否为空,或者缓存中没有加密票
if (ticket == null || Context.Cache[ticket] == null)
{
///用户没有通过验证,返回假
return false;
}
else
{
///解密加密票,获取用户名
string username = FormsAuthentication.Decrypt(ticket).Name;
if (username == null || username == "")
{
///用户名为空返回假
return false;
}
else
{
if (IsAdminCall && !IsAdministrator(username))
return false;
return true;
}
}
}
private bool IsAdministrator(string name)
{
bool Results = false;
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand cmd = new SqlCommand("select IsAdmin from users where name = @name", conn);
cmd.Parameters.Add("@name", SqlDbType.VarChar);
cmd.Parameters[0].Value = name;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
Results = (bool)dr["IsAdmin"];
}
cmd.Dispose();
conn.Dispose();
return Results;
}
#endregion
#region FileList
/// <summary>
/// 取得文件和文件夹列表
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="path">客户端路径</param>
/// <returns>成功返回文件和文件夹列表,否则返回空</returns>
[WebMethod(Description = "取得目录 需要提供合法票据")]
public string GetFile(string ticket,string path)
{
///查看加密票是否有效
if (IsTicketValid(ticket,false))
{
///验证当前用户目录是否存在
if (!Directory.Exists(rootdir + FormsAuthentication.Decrypt(ticket).Name))
{
///如果不存在
///新建用户目录
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name);
///初始化系统目录
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name + "\\个人助理");
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name + "\\我的文档");
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name + "\\我的图片");
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name + "\\我的多媒体");
///返回当前用户目录
string Results = ListShow(rootdir + FormsAuthentication.Decrypt(ticket).Name);
return Results;
}
else
{
string Path = rootdir + FormsAuthentication.Decrypt(ticket).Name + path;
///获取指定文件夹列表并返回
FileInfo file = new FileInfo(Path);
///判断当前是否是文件夹
if ((file.Attributes & FileAttributes.Directory) != 0)
{
string Results = ListShow(Path);
return Results;
}
else
return null;
}
}
else return null;
}
/// <summary>
/// 文件夹结构
/// </summary>
/// <param name="dir">需要列出结构的目录</param>
/// <returns>文件夹结构</returns>
private string ListShow(string dir)
{
DirectoryInfo di = new DirectoryInfo(dir);
DirectoryInfo[] diArr = di.GetDirectories();
string Results = "";
foreach (DirectoryInfo dri in diArr)
{
///如果是文件夹则头上加@
Results = Results + "@" + dri.Name + "|";
}
FileInfo[] SubFiles = di.GetFiles();
foreach (FileInfo fileNext in SubFiles)
{
Results = Results + fileNext.ToString() + "|";
}
return Results;
}
/// <summary>
/// 新建文件夹
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="path">路径</param>
/// <returns>成功返回“success” ,失败返回空</returns>
[WebMethod(Description = "新建目录 需要提供合法票据")]
public string CreateFolder(string ticket, string path)
{
if (IsTicketValid(ticket,false))
{
try
{
if (!Directory.Exists(rootdir + FormsAuthentication.Decrypt(ticket).Name + path))
{
Directory.CreateDirectory(rootdir + FormsAuthentication.Decrypt(ticket).Name + path);
return null;
}
else
return "目录已经存在";
}
catch (Exception ex)
{
CustomSoapException("目录创建失败", "目录创建失败");
return ex.ToString();
}
}
else
return "验证失败";
}
/// <summary>
/// 重命名
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="OldName">原文件名</param>
/// <param name="NewName">新文件名</param>
/// <returns>成功返回“success” ,失败返回空</returns>
[WebMethod(Description = "重命名 需要提供合法票据")]
public string ReName(string ticket, string OldName, string NewName)
{
if (IsTicketValid(ticket,false))
{
string OlePath = rootdir + FormsAuthentication.Decrypt(ticket).Name + OldName;
string NewPath = rootdir + FormsAuthentication.Decrypt(ticket).Name + NewName;
try
{
Directory.Move(OlePath, NewPath);
return "success";
}
catch
{
CustomSoapException("目录创建失败", "目录创建失败");
return null;
}
}
else
return null;
}
/// <summary>
/// 文件文件夹删除
/// </summary>
/// <param name="ticket">加密票</param>
/// <param name="Name">路径</param>
[WebMethod(Description = "删除 需要提供合法票据")]
public string ReMove(string ticket, string Name)
{
if (IsTicketValid(ticket,false))
{
string Path = rootdir + FormsAuthentication.Decrypt(ticket).Name + Name;
try
{
FileInfo file = new FileInfo(Path);
///判断当前是否是文件夹
if ((file.Attributes & FileAttributes.Directory) != 0)
Directory.Delete(Path, true);
else
File.Delete(Path);
return null;
}
catch
{
return "删除失败";
}
}
else
return "验证失败";
}
/// <summary>
/// 属性
/// </summary>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -