Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: wrong redirect to login page when current store is not default #315

Merged
merged 6 commits into from
Oct 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
artem-dudarev marked this conversation as resolved.
Show resolved Hide resolved

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);
}

artem-dudarev marked this conversation as resolved.
Show resolved Hide resolved


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();
}
}
}