Exchange Web Services bind Mailbox folder using FolderID and reflection - reflection

I am trying to load the Exchange Web Services DLL at runtime and connect to a mailbox. I am following this guide: Using Reflection to load unreferenced assemblies at runtime in C#
The code so far:
var DLL = Assembly.LoadFile(#"Microsoft.Exchange.WebServices.dll");
var theType = DLL.GetType("Microsoft.Exchange.WebServices.Data.ExchangeService");
var c = Activator.CreateInstance(theType);
var method = theType.GetMethod("AutodiscoverUrl");
method.Invoke(c, new object[] { #"anyvalid#email.com" });
After that code I am lost. How do I use the ExchangeService to bind a Mailbox object using a FolderId? EWS Managed API is not an option for my server and application.
This is the Powershell script equivalent code that I am trying to implement in ASP.NET:
$MailboxName = "account#domain"
$dllpath = "Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)
$service.AutodiscoverUrl("anyvalid#email.com")
$mbfolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
$MsgRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$mbfolderid)

Using reflection is tedious. You are on the right track though. The following code shows how you can bind to the "Inbox" folder and get the subjects of the first 10 email messages.
Notice how I use the dynamickeyword so I do not have to call MethodInfo.Invoke to call instance methods on the reflected types.
string mailboxName = "...";
// Get value for enum WellKnownFolderName.Inbox.
var wellKnownFolderNameType = assem.GetType("Microsoft.Exchange.WebServices.Data.WellKnownFolderName");
var rootFolderName = wellKnownFolderNameType
.GetField("Inbox")
.GetValue(null)
;
// Create requested mailbox and folderid for Inbox-folder for the requested mailbox.
var mailboxType = assem.GetType("Microsoft.Exchange.WebServices.Data.Mailbox");
dynamic mailbox = Activator.CreateInstance(mailboxType, new object[] { mailboxName });
var folderIdType = assem.GetType("Microsoft.Exchange.WebServices.Data.FolderId");
dynamic folderId = Activator.CreateInstance(folderIdType, rootFolderName, mailbox);
// Bind to the Inbox-folder for the requested mailbox.
var folderType = assem.GetType("Microsoft.Exchange.WebServices.Data.Folder");
var bindMethod = folderType.GetMethod("Bind", new Type[] { serviceType, folderIdType });
dynamic folder = bindMethod.Invoke(null, new object[] { service, folderId });
// Get 10 first mailitems
var itemViewType = assem.GetType("Microsoft.Exchange.WebServices.Data.ItemView");
dynamic itemView = Activator.CreateInstance(itemViewType, 10);
dynamic findItemsResults = folder.FindItems(itemView);
foreach (dynamic item in findItemsResults.Items)
{
Console.WriteLine((string) item.Subject);
}

Related

EPPLUS package works fine locally but returns Internal server error when deployed to azure server

I have my web api that uploads and reads an excel file from the client app and then afterwards saves the data into the database, the application works perfect on locally server but the problem comes when the application is deployed to azure server it returns error 500 internal server error therefore i don't understand why this happens and and don't know how i can track to understand what might be the cause below are my code blocks.
My Interface Class
public interface UploadExcelInterface
{
Task UploadMultipleClients(Client obj);
}
My Service Implementation
public class UploadExcelService : UploadExcelInterface
{
private readonly DbContext _connect;
private readonly IHttpContextAccessor httpContextAccessor;
public UploadExcelService(DbContext _connect, IHttpContextAccessor httpContextAccessor)
{
this._connect = _connect;
this.httpContextAccessor = httpContextAccessor;
}
public async Task UploadMultipleClients(Client obj)
{
var file = httpContextAccessor.HttpContext.Request.Form.Files[0];
if (file != null && file.Length > 0)
{
var folderName = Path.Combine("Datas", "ClientUPloads");
var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
var fileName = Guid.NewGuid() + ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var fullPath = Path.Combine(pathToSave, fileName);
var clientsList = new List<Client>();
using (var fileStream = new FileStream(fullPath, FileMode.Create))
{
await file.CopyToAsync(fileStream);
FileInfo excelFile = new FileInfo(Path.Combine(pathToSave, fileName));
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (ExcelPackage package = new ExcelPackage(excelFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
var rowcount = worksheet.Dimension.Rows;
for (int row = 2; row <= rowcount; row++)
{
var Names = (worksheet.Cells[row,2].Value ?? string.Empty).ToString().Trim();
var Address = (worksheet.Cells[row,3].Value ?? string.Empty).ToString().Trim();
var Title = (worksheet.Cells[row,4].Value ?? string.Empty).ToString().Trim();
var Product = (worksheet.Cells[row,5].Value ?? string.Empty).ToString().Trim();
var Order = (worksheet.Cells[row,6].Value ?? string.Empty).ToString().Trim();
var Email = (worksheet.Cells[row,7].Value ?? string.Empty).ToString().Trim();
var Price = (worksheet.Cells[row,8].Value ?? string.Empty).ToString().Trim();
clientsList.Add(new Client
{
Names = Names,
Address = Address,
Title = Title,
Product = Product,
Order = Order,
Email = Email,
Price = Price,
}
}
//adding clients into the database
foreach (Client client in clientsList)
{
var exist = _connect.client.Any(x => x.Email == client.Email);
if (!exist)
{
await _connect.client.AddAsync(client);
}
}
await _connect.SaveChangesAsync();
}
}
}
My Controller Class
[HttpPost]
public async Task UploadMultipleClients([FromForm] Client obj)
{
await uploadExcelInterface.UploadMultipleClients(obj);
}
}
Please any help regarding this error that am getting from the server, and addition on that is it possible to get the data from the excel file without uploading it to server if yes how? because i tried adding the file to memory stream an reading it from memory but it appers not work, any suggestions thanks.
My answer may not help you solve the problem directly, but it can locate the error step by step. After we fix the error, we should be able to solve the problem in this thread.
Suggestions
Please make sure you have inclue EPPlus library in your deploy content.
Enabling ASP.NET Core stdout log (Windows Server)
Azure App Service - Configure Detailed Error Logging
Why
After tested, I am sure azure webapp can support EPPlus. For 500 error, as we don't have a more specific error message to refer to, we can't quickly locate the problem. Following the suggested method, you will surely see some useful information.
E.g:
The class library of EPPlus was not found.
Folders such as Datas are not created.
The database connection string, the test environment and the production environment may be different.
...

AzureKeyVault Integration AzureFunction and xamarin forms

I have a xamarin app --> azureFunction --->BlobStorage. so far so good.
The AzureFunction is set with AuthorizationLevel.Function.
I have set the azure function Managed identity "ON"
I have assigned a role to the BlobStorage (Blob data Contributor)
I can successfully call the function using postman using the function key.
I would like to store the functionKey in the KeyVault and call it from my mobile app
Question
As anybody got a walkthrough and snippet how to integrate the keyvault with a function key and call it from a mobile app (xamarin forms) c#?
I do not want to hardcode any keys in my mobile app.
I would be very grateful.Lots of googling and nothing.
thanks
Suppose your requirement is call the function from the code. Maybe you could refer to the below code.
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("your Secret Identifier")
.ConfigureAwait(false);
string functionkey = secret.Value;
string functionhost = "https://your function.azurewebsites.net/api/function name";
var param = new Dictionary<string, string>() { { "code", functionkey } ,{ "name","george"} };
Uri functionurl = new Uri(QueryHelpers.AddQueryString(functionhost, param));
var request = (HttpWebRequest)WebRequest.Create(functionurl);
var response = (HttpWebResponse)request.GetResponse();
string responseString;
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
responseString = reader.ReadToEnd();
Console.WriteLine(responseString);
}
}

unable to validate data in Framework 4.6.1

Recently I upgraded a project from Framework 4.6 to 4.6.1. We started facing the following issue:
We combine all the css files to generate a common css file. During this process AjaxControlToolkit Calendar creates a WebResource.axd file. When we try to process this file then the decryption fails and Error is thrown stating : "unable to validate data".
Following code is used:
var queryString = WebResourcePath.Split(new[] { '?' })[1];
var stringBuilder = new StringBuilder();
var textWriter = new StringWriter(stringBuilder);
var context = new HttpContext(new SimpleWorkerRequest("/WebResource.axd", queryString, textWriter));
var urlEncodedData = context.Request.QueryString["d"];
var encryptedData = HttpServerUtility.UrlTokenDecode(urlEncodedData);
var machineKeySection = typeof(MachineKeySection);
var paramTypes = new Type[] { typeof(bool), typeof(byte[]), typeof(byte[]), typeof(int), typeof(int) };
var encryptOrDecryptData = machineKeySection.GetMethod("EncryptOrDecryptData", BindingFlags.Static | BindingFlags.NonPublic, null, paramTypes, null);
try
{
var decryptedData = (byte[])encryptOrDecryptData.Invoke(null, new object[] { false, encryptedData, null, 0, encryptedData.Length });
var decryptedContent = Encoding.UTF8.GetString(decryptedData).Substring(1);
var resourceParts = decryptedContent.Split('|');
Assembly = AssemblyCache.Load(resourceParts[0]);
ResourceName = resourceParts[1];
}
catch (Exception ex)
{
throw ex;
}
Error is thrown on the line : var decryptedData = (byte[])encryptOrDecry........
Some solution I went through suggested use of static machine key but we are already doing it.
NOTE : This is occuring only when we set httpRuntime targetFramework to 4.6.1 otherwise it works as expected.
From forums.asp.net:
So our problem was that some Webresource.axd requests are cached by
browsers (we didn't knew that...) but our machinekeys changes.
Fix the machineKeys in Web.config and wait for all our users to clean
their browser cache.
Comment:
https://forums.asp.net/post/5620791.aspx
Full thread:
https://forums.asp.net/t/1963234.aspx?Unable+to+validate+data+EncryptOrDecryptData+problem
Hope that helps

Get controller and action name from AuthorizationHandlerContext object

Hi I have a custom requirement handler with accepts the AuthorizationHandlerContext context parameter
When i debug, i can see that the context object contains
Context.Resources.ActionDescription.ActionName
But when writing the code i cant go beyond
Context.Resources
Seems the lower levels are not exposed. I want to get the action name and controller name that called the handler. How do i do this?
var mvcContext = context.Resource as AuthorizationFilterContext;
var descriptor = mvcContext?.ActionDescriptor as ControllerActionDescriptor;
if (descriptor != null)
{
var actionName = descriptor.ActionName;
var ctrlName = descriptor.ControllerName;
}
After upgrading to dotnet 5, the solution I was successfully using from Carsten above stopped working. The following workaround now works for me:
var routeValues = (context.Resource as HttpContext).Request.RouteValues;
var controllerName = routeValues["controller"].ToString();
var actionName = routeValues["action"].ToString();
Note this should include some null checks etc. the above is a barebones example.
Even though the question is tagged for asp.net-mvc, I wanted to add that the answer by #AdemCaglin does not work for Web API controllers. The following code works for both, API and MVC controllers:
var endpoint = context.Resource as RouteEndpoint;
var descriptor = endpoint?.Metadata?
.SingleOrDefault(md => md is ControllerActionDescriptor) as ControllerActionDescriptor;
if (descriptor == null)
throw new InvalidOperationException("Unable to retrieve current action descriptor.");
var controllerName = descriptor.ControllerName;
var actionName = descriptor.ActionName;

WCF Client-Server NullReferenceException

Ok, here's the problem i've been dealing with the whole day:
I'm building a web application forms using WCF Data Service and XPO as ORM Data Model in the server side which contains a method, and in the client side, i'm trying to add a line to a database from a web form after authenfitication.
The user can log in without problems with this code:
try
{
ctx = null;
ctx = new XpoContext(serviceRootUri);
ctx.Credentials = new NetworkCredential(UserName.Text, UserPass.Text);
var res = ctx.Users.FirstOrDefault();
FormsAuthentication.RedirectFromLoginPage(UserName.Text, Persist.Checked);
}
catch (SecurityException ex)
{
Msg.Text = "Erreur: " + ex.Message;
}
}
now i want to add a line to a database with this code:
Uri u = new Uri(string.Format(LogIn.ctx.BaseUri + "/CreateUser?name='{0}'&pass='{1}'",
New_UserName.Text, New_UserPass.Text), UriKind.RelativeOrAbsolute);
LogIn.ctx.Execute(u, "GET");
ServiceReference1.Users user = new ServiceReference1.Users();
user.Nom = New_UserName.Text;
user.Pass = New_UserPass.Text;
LogIn.ctx.AddToUsers(user);
LogIn.ctx.SaveChanges();
but after executing in the 1st two lines an exception occurs.
i'm declaring my context as static in the logIn page:
private static Uri serviceRootUri = new Uri("http://localhost:28748/WcfDataService1.svc/");
public static XpoContext ctx;
As u can see i call my context in the adduser web form with LogIn.ctx ( the same context in the log in page )
Thanks

Resources