I have a provider hosted Add-In which looks like that:


In this controller I do the usual OAuth-Permission checks and retrieve the SharePoint-Context using SPTokenhelper

When the user is not authenticated I want to redirect him to the SharePoint/Office365 - login page.

On Office365, the login-page is something like


I do not want to hardcode that login-address as this may differ if it is OnPrem instead of Office365. How can I redirect to the Loginpage?

I want to retrieve the right address programmatically and NOT use a hardcoded Url, because this should work on O365 as well as on OnPrem.

  • Have you done code using CSOM to check permission and generate token? – Viraj Gorajia Apr 20 '16 at 11:59
  • Yes. Through CSOM I detect that the user isnt authenticated (SpContext is null). So I know that the user HAS TO login. But not, where he can do that – Ole Albers Apr 20 '16 at 12:04
  • I don't understand " user HAS TO login. But not, where he can do that ". But you can redirect after SPcontext getting null – Viraj Gorajia Apr 20 '16 at 12:13
  • Yes. But where to? That is what I asked: Where to redirect without using hard-coded urls so this works on SP2013 and O365 – Ole Albers Apr 20 '16 at 13:19
  • From where class is called for code ("detect that the user isnt authenticated (SpContext is null)")? – Viraj Gorajia Apr 21 '16 at 6:19
public abstract class Controller : ControllerBase, IActionFilter, IAuthenticationFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IAsyncManagerContainer
{ }

public class MyController : Controller
    protected override void OnAuthentication(AuthenticationContext filterContext)
        //custom authentication logic

    protected override void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
        //custom authentication challenge logic
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.