?? 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 + -