How to get client remote ip address in resource controller - dart

How can I access request.connectioninfo.remoteaddress in a function of a resourcecontroller? I need to store and log the client remote IP.

Use the raw property of the request https://pub.dartlang.org/documentation/aqueduct/latest/aqueduct/Request/raw.html
Edit by CA:
The request object is already present in a controller that extends from ResourceController or Controller and can simply be used as such:
final remote_address = request.raw.connectionInfo.remoteAddress.address;

You can’t bind it, but you access it the same way. The request is available as a property of a resource controller, so it’s available in every method.

Related

Get user from the middleware in Aqueduct

How to expose information from an authorization middleware, such as the logged-in user to the subsequent request controllers with Aqueduct?
Eg my route is:
.route('/events/[:id]')
.link(() => SecretKeyAuthorizer(context))
.link(() => EventsController(context));
And the SecretKeyAuthorizer uses a header to find the current User. And I'd like to use the information from the User in the EventsController.
After a long search I finally found the answer. One can use attachments that is a dictionary for that purpose in the request object, kept for its lifetime. So typically the Authorizer would do something like:
request.attachments["user"] = user;
And the subsequent RessourceControllers can use it with :
User user = request.attachments["user"] as User;
Note that request, is a member of the "Controller" class, so it is directly accessible through inheritance.

How to force session_id in ASP.Net MVC

I am doing some ajax calls (with jquery) from browser.
I notice that the session id is not sent by the browser.
What I want to do is to pass the session ID as a parameter.
But on server side, i do not know how to tell asp.net "Now, you will use this value as session_id".
In PHP, i was used to do something like that:
session_start($_POST['my_session_id']);
I want to do the same thing in ASP.Net
Thanks
You want a custom ISessionIdManager.
The ISessionIDManager interface identifies the methods that you must implement to create a custom manager for session-identifier values. An ISessionIDManager interface implementation creates and validates session-identifier values, and manages the storage of a session identifier in the HTTP response as well as the retrieval of a session-identifier value from the HTTP request.
[...]
If you only want to supply custom session-identifier values to be used by ASP.NET session state, you can create a class that inherits the SessionIDManager class and override only the CreateSessionID and Validate methods with your own custom implementation. This enables you to supply your own session-identifier values, while relying on the base SessionIDManager class to store values to the HTTP response and retrieve values from the HTTP request.

How to map all requests to a controller go to one method and access the original parameter

I have a controller where I'm trying to set defaults based on the url - but have all of the requests going to one controller.
Trying to extend the answer in : URLMapping to direct all request to a single controller/action
I did this in URLMappings.groovy
"/**"(controller:"lab", action:"index", params:[labName:action])
Where I was hoping I could add the original action name to the parameters, but this doesn't seem to do anything.
Any way I could have all the requests going to that controller mapped to one action, and see what the original action name would be?
Action name is decided based on the url mapping not by the requested url. As you are using a single action, you will always get the action name as index. Based on your requirement below are some of the options that you can choose:
Use requested url and http method to find the right controller and action. Not recommended.
Use filter for setting default data
Use filter to redirect to the default controller after saving the original controller and action in request attributes. Not recommended as it will cause multiple redirects
Extend your controllers with the default controller and do the data setting in interceptor.

How to make the top level endpoint public when authorization is used in eve?

I am working on a REST API with python-eve. I use authorization with a subclass of the default TokenAuth class as described in the documentation. However now a GET Request to / replies with error code 401 and the message "Please provide proper credentials".
I want a GET request to / to just return the default list of available resources without authorization.
For a regular endpoint I would just add GET to the public_methods property in the schema, but / does not have a schema, how can I make it a public endpoint again?
You could go the other way around. Set PUBLIC_METHODS to ['GET'] so home endpoint is accessible. Then you set public_methods to [] for every protected resource.

How to get started with multi-tenant MVC application

I have searched for examples and found several but they are whole large projects. I am looking for some sample on how to get started building an MVC multi-tenant application. I think, the first part would be to decipher the url.
In ASP.Net this is how I did it. I got this from looking at DNN code. How would I do the same in MVC?
Global.asax
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string domainName = string.Empty;
// domaName now contains 'example' if application.Request was www.example.com
domainName = GetDomainName(application.Request);
// Using domain, get the info for example from the database
object myPortal = // get from database
// Save in context for use on other pages
context.Items.Add("PortalSettings", myPortal);
}
Then in my basepage I get the value from the context.
I think an even more robust means would be to define a custom route. In that custom route is where you extract the domain and put it into the route values.
You then can have the base controller (as Josh described) which defines a Domain property or the like and stores that value there for convenience (or just extracts it on demand; either way).
By pulling it into the route values up front like that, you can make use of that information anywhere in the app along the request path, not just in the controller, so you get more re-use out of it that way. You can, for example, make use of it in a custom Authorize-like filter to handle the user's rights to that domain, and so on.
Get the domain name. You are on the right track with the DNN code. Just poke around the Request static variable in the debugger; there's all kinds of cool stuff there.
You'll probably need a user store. I use a custom database, but you could use the Microsoft membership provider and profile provider. Make the domain a property of the user, or a property of an organization, and the organization a property of the user.
Store the user's domain in the cookie, encrypted. Read the cookie at the beginning of the request, and make the user has access to that org/domain.
Make a BaseController that extends Controller, then have all your controllers inherit from it. In the BaseController, override OnActionExecuting. This is a much easier place to do your initial request rigging than the Global.asax.cs's Begin_request, because you can define protected members which will be available form every controller.

Resources