Thursday, September 26, 2019

Unit test controller actions with automapper

When testing controller actions we frequently come across objects which are converted using AutoMapper. While this library is very convenient to use, some extra coding is required in unit tests to test such controller actions. The following article provides a solution for this scenario:

https://stackoverflow.com/questions/49934707/automapper-in-xunit-testing-and-net-core-2-0

In practice, I added a custom mapping profile like so:

//auto mapper configuration
            var mockMapper = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MyMapperProfile());
            });
            var mapper = mockMapper.CreateMapper();

Monday, September 16, 2019

SharePoint Provider Hosted App: AuthenticationContext.HttpContext.User is Null after SharePointContextProvider.CheckRedirectionStatus

The issue was AuthenticationContext.HttpContext.User object is Null after SharePointContextProvider.CheckRedirectionStatus call inside OnAuthentication() method of custom ActionFilterAttribute of a provider hosted app.

public void OnAuthentication(AuthenticationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            bool ok = false;
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(filterContext.HttpContext, out redirectUrl))
            {
                case RedirectionStatus.Ok:
                    ok = true;
                    break;
                case RedirectionStatus.ShouldRedirect:
                    filterContext.Result = new RedirectResult(redirectUrl.AbsoluteUri);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    filterContext.Result = new ViewResult { ViewName = "Error" };
                    break;
            }
            if (ok)
            {
 
                filterContext.Principal = filterContext.HttpContext.User;


Reason for this error was that the app was not registered in the dev server. After installing the app using AppRegNew.aspx this error was solved.

Sharepoint 2013 MVC 5 provider-hosted app. Fails to authenticate using SharePointContextProvider.CheckRedirectionStatus

One of the reasons for this failure is SPHostUrl parameter has not been passed to the post action. It can be passed by several ways


  1. Use Html.BeginForm like so:
ViewBag.SPURL = Request.QueryString["SPHostUrl"];

And if the view

@using (Html.BeginForm("Upload", "Home", new { SPHostUrl = @ViewBag.SPURL }, FormMethod.Post, new { enctype = "multipart/form-data" }))



2. In C# pass the parameter in redirect statement


return RedirectToAction("Index", new {SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri});

References:








SharePoint Provider Hosted App: SPAppWebUrl missing

I faced this problem in one of the sharepoint apps I was working on, a quick search on google returned few pages which suggest a solution:

Add a dummy list to the app: This solution works since sharepoint automatically creates app web for the app if there is some content to be deployed to the web.

More references:

https://stackoverflow.com/questions/18334525/cant-get-spappweburl-sharepoint-2013

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/url-strings-and-tokens-in-sharepoint-add-ins?redirectedfrom=MSDN#Anchor_0

https://social.msdn.microsoft.com/Forums/en-US/7c417bb9-4456-4f67-a71b-97928369e040/how-to-add-standardtokens-to-aspx-page?forum=appsforsharepoint

https://sharepoint.stackexchange.com/questions/104063/sharepoint-app-spappweburl-query-string-is-missing-after-using-sharepointcont


SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...