Sunny
Sunny

Reputation: 4809

Custom authorization based on url in ASP.NET MVC

I am saving url's in database and respective users are mapped to the url

URL: ~/user

From application, I am checking via

bool isAuthorized = DBHelper.IsAuthorized(
string.Concat("~" , Url.Action("Index", "User"), 
httpContext.User.Identity.Name);

It works well in development environment. But in production, application is configured under a IIS application ucms, so Url.Action("Index", "User") is returning /ucms/user henceisAuthorized is returning false.

Could anyone provide any inputs or direction on the changes to be done to tackle this behaviour.

Upvotes: 0

Views: 232

Answers (1)

jgauffin
jgauffin

Reputation: 101150

Remove the virtual path before checking it:

var relativePath = yourPath.Remove(0, HttpContext.Current.Request.ApplicationPath.Length);

Works both for virtual dirs and domains.

Upvotes: 1

Related Questions