-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathToolsController.cs
More file actions
141 lines (126 loc) · 3.91 KB
/
Copy pathToolsController.cs
File metadata and controls
141 lines (126 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using App_Code;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.UI.HtmlControls;
public class ToolsController : ApiController
{
private static object _SyncRoot = new object();
public HttpResponseMessage Get(string id)
{
if (id == "check1")
{
var result = string.Format("ASP.NET application Identity is: {0}", WindowsIdentity.GetCurrent().Name);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
else if (id == "trust")
{
var trust = GetCurrentTrustLevel();
if (trust == AspNetHostingPermissionLevel.High || trust == AspNetHostingPermissionLevel.Unrestricted)
{
var data = new { success = true, msg = "Trust level: " + trust.ToString() };
return Request.CreateResponse(HttpStatusCode.OK, data);
}
else
{
var data = new { success = false, msg = "Trust level: " + trust.ToString() };
return Request.CreateResponse(HttpStatusCode.OK, data);
}
}
else
{
string fileName = GetFileName(id);
var processed = Process(fileName);
if (string.IsNullOrEmpty(processed))
{
var data = new { success = true, msg = "Can create and delete file " + fileName };
return Request.CreateResponse(HttpStatusCode.OK, data);
}
else
{
var data = new { success = false, msg = "Error creating file " + fileName };
return Request.CreateResponse(HttpStatusCode.OK, data);
}
}
}
#region Private methods
private string Process(string file)
{
string processed = WriteToFile(file);
if (!string.IsNullOrEmpty(processed))
return processed;
return DeleteFile(file);
}
private string WriteToFile(string file)
{
lock (_SyncRoot)
{
try
{
using (FileStream fs = new FileStream(file, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(@"*-*-*");
sw.Close();
fs.Close();
return "";
}
}
}
catch (Exception e)
{
return e.Message;
}
}
}
private string DeleteFile(string file)
{
try
{
File.Delete(file);
return "";
}
catch (Exception e)
{
return e.Message;
}
}
private static string GetFileName(string dir)
{
if(dir == "root")
return HostingEnvironment.MapPath("~/tmp.txt");
if (dir == "data")
return HostingEnvironment.MapPath("~/App_Data/tmp.txt");
return HostingEnvironment.MapPath($"~/{dir}/tmp.txt");
}
AspNetHostingPermissionLevel GetCurrentTrustLevel()
{
foreach (AspNetHostingPermissionLevel trustLevel in new AspNetHostingPermissionLevel[]
{
AspNetHostingPermissionLevel.Unrestricted,
AspNetHostingPermissionLevel.High,
AspNetHostingPermissionLevel.Medium,
AspNetHostingPermissionLevel.Low,
AspNetHostingPermissionLevel.Minimal
})
{
try
{
new AspNetHostingPermission(trustLevel).Demand();
}
catch (System.Security.SecurityException)
{
continue;
}
return trustLevel;
}
return AspNetHostingPermissionLevel.None;
}
#endregion
}