public ActionResult Logout(string returnUrl) { var cookieAuthenticationEnabled = !string.IsNullOrEmpty(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")) && Convert.ToBoolean(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")); if (cookieAuthenticationEnabled) { if (Request.IsAuthenticated) { SPContext spContext = SPContextProvider.Get(HttpContext.User as ClaimsPrincipal, false); HttpContext.GetOwinContext().Authentication.SignOut(SPAddinAuthenticationDefaults.AuthenticationType); if (spContext.SPAppWebUrl != null) { return(new RedirectResult($"{spContext.SPAppWebUrl.GetLeftPart(UriPartial.Path).TrimEnd('/')}/_layouts/closeConnection.aspx?loginasanotheruser=true")); } } } else { Uri spHostUrl = GetSPHostUrl(returnUrl); if (spHostUrl == null) { spHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request); } var spContext = SharePointContextProvider.Current.GetSharePointContext(); if (spContext != null) { Uri appWebUrl = spContext.SPAppWebUrl; if (appWebUrl != null) { return(new RedirectResult($"{appWebUrl.GetLeftPart(UriPartial.Path).TrimEnd('/')}/_layouts/closeConnection.aspx?loginasanotheruser=true")); } } } return(new RedirectResult($"/login?ReturnUrl={HttpUtility.UrlEncode(returnUrl)}")); }
// GET: Home public ActionResult Index() { var spContext = SPContextProvider.Get(User as ClaimsPrincipal); using (var ctx = spContext.CreateUserClientContextForSPHost()) { ctx.Load(ctx.Web.CurrentUser); ctx.ExecuteQuery(); } return(View()); }
public string Get() { var spContext = SPContextProvider.Get(User as ClaimsPrincipal); using (var ctx = spContext.CreateUserClientContextForSPHost()) { ctx.Load(ctx.Web); ctx.ExecuteQuery(); return(ctx.Web.Title); } }
public Task ValidateIdentity(CookieValidateIdentityContext context) { if (context.Identity.IsAuthenticated) { if (context.Request.Path.Value.Contains(context.Options.LoginPath.Value)) { return(Task.FromResult <object>(null)); } bool isWebPart = context.Request.Get <string>("IsWebPart") == "1"; var spContext = SPContextProvider.Get(context.Identity, isWebPart); string spHostUrlString = TokenHelper.EnsureTrailingSlash(context.Request.Query.Get(SharePointContext.SPHostUrlKey)); if (string.IsNullOrEmpty(spHostUrlString)) { spHostUrlString = ConfigurationManager.AppSettings["SPHostUrl"]; } Uri spHostUrl; if (!Uri.TryCreate(spHostUrlString, UriKind.Absolute, out spHostUrl)) { //throw new Exception(string.Format("Unable to determine {0}.", SharePointContext.SPHostUrlKey)); } //try //{ if (spHostUrl != null && !string.Equals(spContext.SPHostUrl.GetLeftPart(UriPartial.Path).TrimEnd('/'), spHostUrl.GetLeftPart(UriPartial.Path).TrimEnd('/'), StringComparison.OrdinalIgnoreCase)) { context.RejectIdentity(); } //} //catch (Exception) //{ // context.RejectIdentity(); //} string clientId = ConfigurationManager.AppSettings["ClientId"]; try { if (spContext.ClientId != (string.IsNullOrEmpty(clientId) ? Guid.Empty : new Guid(clientId))) { context.RejectIdentity(); } } catch (Exception) { context.RejectIdentity(); } } return(Task.FromResult <object>(null)); }
public IActionResult Index(string shortUrl) { var spcontext = SPContextProvider.Get(User); using (var clientContext = spcontext.CreateUserClientContextForSPHost()) { clientContext.Load(clientContext.Web.CurrentUser); clientContext.Load(clientContext.Web); clientContext.ExecuteQuery(); ViewBag.User = clientContext.Web.CurrentUser.LoginName; ViewBag.Host = clientContext.Web.Title; } return(View()); }
public static ISPContext GetSPContext(HttpContextBase httpContext) { ISPContext context; var cookieAuthenticationEnabled = !string.IsNullOrEmpty(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")) && Convert.ToBoolean(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")); bool isWebPart = httpContext.Request["IsWebPart"] == "1"; if (cookieAuthenticationEnabled) { context = SPContextProvider.Get(httpContext.User as System.Security.Claims.ClaimsPrincipal, isWebPart); } else { Uri spHostUrl = SharePointContext.GetSPHostUrl(httpContext.Request); if (spHostUrl == null) { string spHostUrlString = WebConfigurationManager.AppSettings.Get(SharePointContext.SPHostUrlKey); if (!Uri.TryCreate(spHostUrlString, UriKind.Absolute, out spHostUrl)) { } } context = SharePointContextProvider.Current.GetSharePointContext(); } return(context); }
protected override bool AuthorizeCore(HttpContextBase httpContext) { var cookieAuthenticationEnabled = !string.IsNullOrEmpty(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")) && Convert.ToBoolean(WebConfigurationManager.AppSettings.Get("CookieAuthenticationEnabled")); bool authorized = !cookieAuthenticationEnabled || base.AuthorizeCore(httpContext); if (authorized) { if (!string.IsNullOrEmpty(SPGroup) || Permissions != PermissionKind.EmptyMask || SiteAdminRequired) { ClientContext clientContext = null; if (cookieAuthenticationEnabled && httpContext.User.Identity.IsAuthenticated) { var spContext = SPContextProvider.Get(httpContext.User.Identity as ClaimsIdentity, false); if (spContext != null) { clientContext = spContext.CreateUserClientContextForSPHost(); } } else { var spContext = SharePointContextProvider.Current.GetSharePointContext(); if (spContext != null) { clientContext = spContext.CreateUserClientContextForSPHost(); } } if (clientContext != null) { User user = clientContext.Web.CurrentUser; ClientResult <bool> hasPermissions; List <Func <bool> > checkers = new List <Func <bool> >(); if (SiteAdminRequired) { clientContext.Load(user, u => u.IsSiteAdmin); checkers.Add(() => user.IsSiteAdmin); } if (!string.IsNullOrEmpty(SPGroup)) { var groups = clientContext.LoadQuery(user.Groups.Include(g => g.LoginName)); checkers.Add(() => { return(groups.Any(g => g.LoginName == SPGroup)); }); } if (Permissions != PermissionKind.EmptyMask) { var perm = new BasePermissions(); perm.Set(Permissions); hasPermissions = clientContext.Web.DoesUserHavePermissions(perm); checkers.Add(() => hasPermissions.Value); } if (checkers.Count > 0) { clientContext.ExecuteQuery(); authorized = checkers.All(c => c()); if (!authorized) { throw new UnauthorizedAccessException(); } } } } } return(authorized); }