MVC 3 Redirect to Action on Subdomain - asp.net-mvc-3

I have a website I am developing (http://www.mywebsite.com/) that makes use of subdomains. Basically what I want is when you go to: http://www.mywebsite.com/Redirect, it redirects you to http://blahblah.mywebsite.com/SpecificController/SpecificAction.
I know about Redirect, but that doesn't let you send POST parameters (as far as I know), and RedirectToAction doesn't let you specify a subdomain.

For subdomains in routes check this guide to domain routing
When you have it set up. for a specific redirection you can use this:
return RedirectToAction("SpecificAction",
"SpecificController",
new { subdomain = "blahblah");
As for the post part, you can just use the TempData dictionary (TempData["varName"]) to pass the data to the next controller/action

Related

Redirecting to a Laravel Spark Settings Page

In Laravel Spark, settings pages have URLs that look like this
http://app.dev/settings#/security
Is there a way, via Laravel's PHP code (from a controller action) to redirect to these URLs that doesn't involve manually adding the # portion to the URL via string concatenation?
In other works, I know I can do this
return redirect(
route('settings') . '#/security'
);
but it feels sort of gross. I guess another way of asking this question is, is there a built-in way to generate route based URLs in laravel that include the # portion of a URL.

Encrypt URL with PHP Codeigniter

I have developed a website with PHP Codeigniter. It consists of many controllers and views. I want to encrypt the URL of the site to hide the controller and function name. Currently, the URL looks like this :
www.sitename.com/controller_name/function_name
I don't want the users to see the controller and function names. Is it possible to do this now because the site is almost complete. Is there any shortcut of doing this? Please Help
You can use codeigniter URI Routing to remove your controller name from URL.
$route['url_name'] = 'controller_name/function_name';
This `www.sitename.com/url_name` will look go to `www.sitename.com/controller_name/function_name`
$route['url_name/(:any)'] = 'controller_name/$1';
This will only change your controller name.. `controller_name` to `url_name`

Routing problems, CodeIgniter

This might be a very silly question because I'm new to PHP frameworks. But I have got the concept of MVC framework. But this routing thing is very confusing me.
I created a controller and view for dashboard and set it as default. Then I wanted to check if the user is logged in then send him to the login page if it's not.
So, what I did in the routes is this:
$route['default_controller'] = 'dashboard/index';
$route['login'] = 'login';
But even if I add a thousand more controllers and routes, it still goes to the default i.e the dashboard. This was supposed to work http://localhost/codeigniter/login. I'm very sure I haven't put a redirect-to-dashboard code in the login page.
at first it's important to understand to routing in codeigniter.
The key for understanding is $route['url/path'] = controller/method (or "public function")
So. In your case, you routing to Login controller and index method (or index function...this it interpretation of codeigniter )
If you want to check if is user logged or not, you may try to check for examle session variable inside of called functon. But it depends on your application. But I think that is not good idea to check it in routes.php
Hope it helps you.
The default controller in routes, will be the controller that opens your index or first page.
If your first page is in controller "pages", then that is your default controller. Do not use it for your secure dashboard pages
Your first page should be the index method in the controller. It should look like this
$route['default_controller'] = "pages";
Where pages is your first page controller

How do I change the index page in a CodeIgniter anchor?

So, I have two different applications in my CodeIgniter installation. One is admin, the other is frontend. I basically just copied the index file, renamed it "admin.php", and changed the application directory to "application/admin". I then changed the application directory in index.php to "application/frontend".
What I would like to do is create a link on the frontend application that takes you to the admin application. The variable config['index_page'] in the frontend application is set to "index.php" and in the admin application it's set to "admin.php".
Is there a way to set the url helper to use "admin.php" instead of "index.php"?
You don't need to do that way.
you should make or use an authentication library and you set different roles for different
users.
you just after login can put the redirection to your admin controller.
and for other users and viewers you can redirect them to any other controllers.
for viewers you can just use something like this:
Code:
if(!$this->m_auth->is_logged_in())
{
$this->viewers();
}
else
{
$this->users();
}
In your users function you just check different roles and redirect according.
I think you are missing some codeigniter concept, and you are trying to do it the normal way, i suggest you to read this article , you will how you can use MY_Controller as same concept of front controller and how you will be able to give every use specific roles
another way is to use a ready made authentication library as #medhi said
I would recommend Tank Authentication or Ion Auth
I

Login redirects to login after logging in? Microsoft MVC framework

When I first visit my site using URL: http://mysite.com/myapp, I am redirected to the login page, but the returnUrl is "/myapp".
How do I ensure that upon first visit, it redirects to the login page, but the return URL is something other than the login page itself, such as "~/Home/Index".
I have tried adding a routes.MapRoute, passing an empty string to the "url" parameter, and in the object passed to "defaults" I include a value for the returnUrl member as "~/Home/Index", but that's not affecting anything.
I've tried creating a default document, and ensuring full anonymous access to it an the login page, but that's not working either... it always redirects and appends a returnUrl of the login page itself, which is dumb. This seems to be a problem with the default behavior of the routing system when property security is in place.
I figured it out.
The default IIS security mechanism for securing pages (i.e. the system.web/authorization section) is useless when using MVC, because with MVC you're not securing actual physical pages or paths, but rather controllers and actions referenced through virtual paths called "routes".
Instead of being able to secure everything with one line in web.config, you have to remember to add an [Authorize] attribute to every action you want secured (or to each controller class on which you want all actions secured), because everything is accessible by default. This means your security is code-based in MVC, rather than web.config-based, so you cannot change the settings without recompiling.
The exact problem here was that I was securing the app root directory with the authorization section, and was unable to add exceptions for physical sub-paths... because there really are none in MVC.. it's all handled by the routing module. Therefore, when visiting the root with no exceptions possible, the forms authentication module was kicking in saying "no, you cannot access the root path" and was immediately serving the login page and adding a redirect back to the secured root.
Normally, having the default route go to Account/LogOn, would be fine, because Account/LogOn has code to redirect to Home/Index in the absense of a returnUrl. However, in this case, the authorization section was causing a returnUrl to be generated by forms authentication, which sabataged the ability of the Account/LogOn action to perform a default redirect. Changing the default route to Home/Index fixed that problem; however this left the browser's URL incorrect, and that's when I ran into the problem of URL's being specified as "../Content/image.jpg", rather than #Url.Content("~/Content/image.jpg").
So as you can see, this was a complex, multifaceted problem here that arose from a clash of architectures.
See: Problem with Authorization with IIS and MVC
and
See: How do I allow all users access to one route within a website with integrated auth?
To summarize, good suggestions were 1: default route should go to home rather than login, 2: ensure you let the framework generate content URLs relative to application root, 3: add to the http-get action of the login page a redirect to home if Request.IsAuthenticated is true, and 4: use [Authorize] attribute to secure action methods.
You should not have to modify your routing to get this to work correctly. For your "authentication" required actions ensure they are decorated with the [Authorize] attribute.
With forms authentication this will redirect users to the loginUrl specified in your config:
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx" />
</authentication>
</system.web>
After authentication occurs, information about the originating page will be placed in the query string using RETURNURL as the key.
Edit:
Within the /myapp controller's action result check to see if the user is authenticated via Request.IsAuthenticated. If authenticated and the return url is "/myapp" (second check may not be necessary), redirect them over to the desired controller / action.
Edit2:
The default controller / action is specified in your global.asax. For example:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This should be where you are redirecting to with RedirectToRouteResult.

Resources