public async Task <IActionResult> StoreData([FromBody] StoreSessionDataRequest request) { if (request == null) { return(new NotFoundResult()); } if (string.IsNullOrEmpty(request.Key)) { return(new NotFoundResult()); } if (!string.IsNullOrEmpty(request.Data) && request.Data.Length > 4096) { return(new NotFoundResult()); } var spa = _externalSpaStore.GetRecord(request.Key); if (spa == null) { return(new NotFoundResult()); } var key = $".extSpa.Session.{request.Key}"; SessionCacheManager <string> .Insert(_httpContextAccessor.HttpContext, key, request.Data); return(new OkResult()); }
public override void OnActionExecuting(ActionExecutingContext context) { object key; if (!context.ActionArguments.TryGetValue("id", out key)) { context.Result = new UnauthorizedResult(); } else { var sKey = key as string; var spa = _externalSPAStore.GetRecord(sKey); if (spa == null) { context.Result = new UnauthorizedResult(); } else { if (spa.RequireAuth) { if (!context.HttpContext.User.Identity.IsAuthenticated) { context.Result = new RedirectToActionResult(Action, Controller, new { area = Area, returnUrl = context.HttpContext.Items["original-path"] }); } else { bool requireLogin = false; if (spa.StrongLoginRequiredSeconds > 0) { var strongLoginUtc = _httpContextAccessor.HttpContext.Session.GetObject <DateTimeOffset>(".identity.strongLoginUtc"); DateTimeOffset future = strongLoginUtc.AddSeconds(spa.StrongLoginRequiredSeconds); if (future <= DateTimeOffset.UtcNow) { requireLogin = true; context.HttpContext.Response.Cookies.Append(".LoginHint", "Soft", new CookieOptions() { HttpOnly = false }); context.Result = new RedirectToActionResult(Action, Controller, new { area = Area, returnUrl = context.HttpContext.Items["original-path"] }); } } if (!requireLogin) { var result = from claim in context.HttpContext.User.Claims where claim.Type == ClaimTypes.NameIdentifier || claim.Type == "name" select claim; if (!result.Any()) { context.Result = new UnauthorizedResult(); } } } } } } base.OnActionExecuting(context); }
public IActionResult Index(string id) { Logger.LogInformation("Hello from the External SPA Home Index Controller"); var spa = _externalSpaStore.GetRecord(id); var result = HttpContext.User.Claims.Select( c => new ClaimType { Type = c.Type, Value = c.Value }); // var model = new HtmlString(spa.RenderTemplate); return(View(spa.View, result)); }
public override void OnActionExecuting(ActionExecutingContext context) { if (!context.HttpContext.User.Identity.IsAuthenticated) { object key; if (!context.ActionArguments.TryGetValue("id", out key)) { context.Result = new UnauthorizedResult(); } else { var sKey = key as string; var spa = _externalSPAStore.GetRecord(sKey); if (spa == null) { context.Result = new UnauthorizedResult(); } else { if (spa.RequireAuth) { context.Result = new RedirectToActionResult(Action, Controller, new { area = Area, returnUrl = context.HttpContext.Items["original-path"] }); } } } } else { var result = from claim in context.HttpContext.User.Claims where claim.Type == ClaimTypes.NameIdentifier select claim; if (!result.Any()) { context.Result = new UnauthorizedResult(); } } base.OnActionExecuting(context); }
public async Task <IActionResult> Index(string id) { Logger.LogInformation("Hello from the External SPA Home Index Controller"); var spa = _externalSpaStore.GetRecord(id); if (spa == null) { return(new NotFoundResult()); } var loadedSpas = SessionCacheManager <Dictionary <string, ExternalSPARecord> > .Grab(_httpContextAccessor.HttpContext, _loadedSpasKey) ?? new Dictionary <string, ExternalSPARecord>(); var result = HttpContext.User.Claims.Select( c => new ClaimType { Type = c.Type, Value = c.Value }); var cacheKey = $".extSpaViewBagRecord.{id}"; ViewBagRecord viewBagRecord = null; var value = await _cache.GetAsync(cacheKey); if (value != null) { viewBagRecord = ZeroFormatterSerializer.Deserialize <ViewBagRecord>(value); } else { var doc = await _discoveryCache.GetAsync(); var request = new AuthorizeRequest(doc.AuthorizeEndpoint); var url = request.CreateAuthorizeUrl( clientId: spa.ClientId, responseType: OidcConstants.ResponseTypes.Code, prompt: OidcConstants.PromptModes.None, redirectUri: spa.RedirectUri, scope: "openid profile email"); var mySpaRecord = new MySpaRecord() { ClientId = spa.ClientId, Key = spa.Key, RedirectUri = spa.RedirectUri, CacheBustHash = spa.CacheBustHash }; viewBagRecord = new ViewBagRecord { AuthorizeEndpoint = doc.AuthorizeEndpoint, AuthorizeUrl = url, SpaRecord = mySpaRecord }; var val = ZeroFormatterSerializer.Serialize(viewBagRecord); var cacheEntryOptions = new DistributedCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(5)); _cache.Set(cacheKey, val, cacheEntryOptions); } ViewBag.ViewBagRecord = viewBagRecord; if (!loadedSpas.ContainsKey(id)) { loadedSpas.Add(id, spa); SessionCacheManager <Dictionary <string, ExternalSPARecord> > .Insert(_httpContextAccessor.HttpContext, _loadedSpasKey, loadedSpas); } var key = $".extSpa.Session.{viewBagRecord.SpaRecord.Key}"; var customData = SessionCacheManager <string> .Grab(_httpContextAccessor.HttpContext, key); ViewBag.CacheBustHash = viewBagRecord.SpaRecord.CacheBustHash; ViewBag.CustomData = customData; return(View(spa.View, result)); }