Skip to content

Commit

Permalink
Fix: wrong redirect to login page when current store is not default (#…
Browse files Browse the repository at this point in the history
…315)

* fix wrong redirect to login page when current store is not default

* little changes

* edit after code review

* rename method AddStoreAndLanuageToUri to GetAbsoluteUri

* fix GetAbsoluteUri calling

* Code cleanup
  • Loading branch information
trueboroda authored and tatarincev committed Oct 7, 2019
1 parent ec1fe2d commit 872bf25
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using VirtoCommerce.Storefront.Extensions;
using VirtoCommerce.Storefront.Model.Common;

namespace VirtoCommerce.Storefront.Domain.Security
{
public class CustomCookieAuthenticationEvents : CookieAuthenticationEvents
{
private readonly IStorefrontUrlBuilder _storefrontUrlBuilder;

public CustomCookieAuthenticationEvents(IStorefrontUrlBuilder storefrontUrlBuilder)
{
_storefrontUrlBuilder = storefrontUrlBuilder;
}

public override Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context)
{
if (context.Request.Path.IsApi())
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Task.CompletedTask;
}

context.RedirectUri = GetStoreAbsoluteUri(context.RedirectUri);

return base.RedirectToLogin(context);
}

Expand All @@ -25,8 +37,20 @@ public override Task RedirectToAccessDenied(RedirectContext<CookieAuthentication
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return Task.CompletedTask;
}

context.RedirectUri = GetStoreAbsoluteUri(context.RedirectUri);

return base.RedirectToAccessDenied(context);
}



private string GetStoreAbsoluteUri(string uri)
{
//Need to build from an host absolute url a relative store-based url
// http://localhost/Account/Login -> http://localhost/{store}/{lang}/Account/Login
var redirectUri = new UriBuilder(uri);
redirectUri.Path = _storefrontUrlBuilder.ToAppAbsolute(redirectUri.Path);
return redirectUri.Uri.ToString();
}
}
}

0 comments on commit 872bf25

Please sign in to comment.