In preivous projects, the code was somewhat outdated. Here's an optimizaton for recording pruposes.
Instead of checking login status with if statements on every method request, we now utilize attributes to validate permissions during the global file processing phase.
Here’s a demonstration of how it works:
The setup within the global file is as follows:
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current != null)
{
byte[] dataBytes = new byte[HttpContext.Current.Request.InputStream.Length];
HttpContext.Current.Request.InputStream.Read(dataBytes, 0, dataBytes.Length);
string requestData = System.Text.Encoding.UTF8.GetString(dataBytes);
requestData = HttpContext.Current.Server.UrlDecode(requestData);
if (!string.IsNullOrEmpty(requestData))
{
requestData = requestData.Replace("data=", "");
var requestModel = Utils.JsonHelper.FromJson<ApiRequestModel>(requestData); // Convert request stream to JSON
string methodName = requestModel.MethodIdentifier;
var className = ApiCache.GetClassName(methodName);
string assemblyIdentifier = "MyApp.Module";
if (!String.IsNullOrEmpty(assemblyIdentifier) && !String.IsNullOrEmpty(className))
{
Assembly moduleAssembly = LoadAssembly(assemblyIdentifier); // Utilize caching for resource loading
Type classType = moduleAssembly.GetType(className, true, true);
if (classType != null)
{
MethodInfo[] methodDetails = classType.GetMethods();
foreach (MethodInfo methodInfo in methodDetails)
{
System.Attribute[] attributes = System.Attribute.GetCustomAttributes(methodInfo); // Reflect custom attributes
foreach (System.Attribute attribute in attributes)
{
if (attribute is AuthRequiredAttribute)
{
AuthRequiredAttribute authAttr = (AuthRequiredAttribute)attribute;
Console.WriteLine("Access Granted? ", authAttr.IsAuthenticated); // Process or ignore based on requirement
}
}
}
}
}
}
}
}
/// <summary>
/// Cache-based assembly retrieval
/// </summary>
/// <param name="assemblyName"></param>
/// <returns></returns>
private static Assembly LoadAssembly(string assemblyName)
{
object cachedAssembly = CacheManager.GetCache(assemblyName); // Use IIS cache for efficiency
if (cachedAssembly == null)
{
Assembly assemblyInstance = null;
assemblyInstance = Assembly.Load(assemblyName);
CacheManager.SetCache(assemblyName, assemblyInstance, DateTime.Now.AddMinutes(60));
return assemblyInstance;
}
else
{
return (Assembly)cachedAssembly;
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class AuthRequiredAttribute : Attribute
{
public bool IsAuthenticated { get; set; }
public AuthRequiredAttribute()
{
try
{
if (true) // Simplified condition check
{
IsAuthenticated = true;
// Uncomment and handle exceptions or error messages here
}
else
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("{State:1,Msg='Not Logged In'}");
HttpContext.Current.Response.End();
}
}
catch (Exception ex)
{
ErrorLogger.WriteErrorLog("AuthRequiredAttribute", ex);
throw;
}
}
}