Is there an alternative to C# Thread.CurrentThread.CurrentCulture in Java? If not what is the best place to store user selected locale in Java?
Locale.setDefault() sets the default locale for the whole JVM, which is fine for a client application. For a web application, it's not a valid solution, but a thread-local is not either, since the same threads are used for all the users. In this case, the user locale is typically stored in the HTTP session.
If you have web SpringMVC based web application there's this class org.springframework.context.i18n.LocaleContextHolder (javadoc here), which gives you access to the current locale.
In webapplication this may be initialized on the basis of accept-language request header (javadoc here) in DispatcherServlet.
Related
I have some kind of a job scheduling implemented which calls a function ProcessJob. Now inside this method I need to generate url to one of my pages i.e DoanloadPage.aspx?some_params. That url is sent to user via email and when user clicks that link, it will take to the page.
The problem here is that I am not generating url in a web request method or I don't have access to the Request object. URL need to be generated in a custom class which is threaded i.e not in a web request.
So I can't go with these solutions:
HostingEnvironment.MapPath("test.aspx");
VirtualPathUtility.ToAbsolute("123.aspx");
HttpContext.Current.Request.Url.Authority;
None of these works because I think they all rely on current request or session somehow. So how do I generate urls for my app inside my code so I can use them anyway I want.
If your method cannot use HttpContext.Current.Request.Url, for example in case it's a background scheduled task, then you can use either of the following options:
In case that your code is hosted in the same ASP.NET application, you can pass the site domain name of the site to your class, in the first request. To do so, you need to handle Application_BeginRequest event and get the domain from HttpContext.Current.Request.Url and then pass it to your class, or store it in an application scope storage. You can find an implementation in this post or the original article.
Note: The code is available in SO, so I don't repeat the code
here.
If your code is not hosted in the same ASP.NET application or if for any reason you don't want to rely on Application_BeginRequest, as another option you can store the site domain name in a setting (like appsettigs in app.condig or web.config if it's web app) and use it in your code.
You can do something like this. Dns.GetHostName will return the name of the computer that is hosting the site. You can use that to check if the site is on a development server.
string domain = "www.productionurl/123.aspx";
if (Dns.GetHostName() == "Development")
{
domain = "www.developmenturl/123.aspx";
}
The Dns.GetHostName() is not the only way to check. You could also use the HostingEnvironment.ApplicationPhysicalPath. You can check that also and see if the path is that of the development server.
My answer is: don't do this. You're building a distributed system, albeit a simple one, and generally speaking it is problematic to introduce coupling between services in a distributed system. So even though it is possible to seed your domain using Application_BeginRequest, you are then tying the behavior of your batch job to your web site. With this arrangement you risk propagating errors and you make deployment of your system more complicated.
A better way to look at this problem is to realize that the core desire is to synchronize the binding of your production site with the URL that is used in your batch job. In many cases an entry in the app.config of your batch would be the best solution, there really isn't any need to introduce code unless you know that your URL will be changing frequently or you will need to scale to many different arbitrary URLs. If you have a need to support changing the URL programmatically, I recommend you look at setting up a distributed configuration system like Consul and read the current URLs from your deployment system for both the IIS binding and the app.config file for your batch. So even in this advanced scenario, there's no direct interaction between your batch and your web site.
I am currently making a multilingual website in Umbraco (C#/Razor MVC CMS).
I have 3 languages, all have their own domain name, but all point to the same Umbraco installation.
For example:
multilingualwebsite-fr.com
multilingualwebsite-en.com
multilingualwebsite-de.com
When the user picks a different language from the dropdown the site redirects to the domain associated with that language which tells the CMS to switch the language.
I would like to transfer a small amount of data between these domains regarding the user, i.e if they have accepted a pop-up or closed the cookie bar.
I have looked at using cookies and sessions but it has become apparent that these only work on the same domain they are created on so cannot transfer data between each language site. So I cannot read the cookie/session data once the language has been switched.
I have added parameters to the url when the language is switched so the URL looks something like
multilingualwebsite-fr.com?nopopup=true&cookieaccepted=true
but I find these parameters untidy and it is likely that I am going to need to pass more information in the future and this will just get longer.
Are there any better ways of transferring data from one domain to another that I have not thought of?
In hindsite having multilingualwebsite.com/fr/ style domains would have excluded this issue but it isn't want the client wants for their website.
Modern browsers are very restrictive when it comes to cross-domain communication. You are better off doing something on the server-side.
If all the domains point to the same actual application, then you can just store something in memory when you know the user will be switching languages.
Something like this:
User clicks new language.
Send AJAX request to current domain to tell the server the user is changing languages.
Server saves user information in memory.
Browser redirects to new language domain.
Server authenticates user at new language domain.
Server checks for stored user information, and if found, uses it.
If each language domain is a different application, then the server can make an HTTP call to the new language domain to tell it that the user is coming.
I am getting some special characters and some letters along with the url at runtime. The url is like
http://b1.elenageosystems.com/%28S%28z1qmpcpllhmuoxgsnkhcbflh%29%29/Default.aspx
Here you can see the special characters appended along with the url after b1.elenageosystems.com/.....
Default.aspx is my page
You can view my project - www.b1.elenageosystems.com
This is so-called Session ID that used by IIS to manage your sessions, it is added to every URL firing from the same browser session.
Many IIS-based frameworks (for example classical ASP.NET) depend heavily on sessions and require this ID to overcome generale stateless nature of HTTP requests, this is why IIS provides (and implements) fully transparent support for it.
If you don't like these URLs, you can also use Cookies to store your Session ID. You can configure the way how you store your IDs for sessions either per WebApplication using your web.config file in your application or globally for your complete IIS server.
You find more how to change these settings on MSDN.
Requirement:
We have created DQS Knowledgebase, Domains, Domain Values/Rules using DQS client provided by SQL.
Now, we need to invoke DQM Knowledgebase, domain values , Rules from .Net application using an API. User should be able to provide input values and select the Domain, Value/Rule. User should get corrected Output value with attributes such as confidence level on operation execution
We are able to achieve this using SSIS but we are looking for APIs which can be used in ASP .net client application.
Approach: So far Approach we have taken is, configure Master Data Manager web service, this allows programmatic use of the features of Master Data Service (DQS can be integrated with Master data service).
This WCF service have set of Data Quality Service Operations such as DataQualityCleansingOperationCreate, DataQualityKnowledgeBasesGet, DataQualityMatchingOperationCreate, DataQualityOperationStart, DataQualityOperationResultsGet.
Reference: http://msdn.microsoft.com/en-in/library/hh230995.aspx
Problem:
we are not able identify the exact implementation sequence of these operations. We are trying to find out,
How we can provide Input values to these operations, How to select the Operation to perform, How to associate Domain, values, rules, references to operation. How to read the result object.
Any help/pointers or alternative approach are appreciated.
Thanks in advance.
Om
You cannot programmatically use/access DQS functionality because its not supported in the current version of DQS.
This fact that DQS will not provide any public APIs in SQL Server 2012 is clearly documented in Q.12 of DQS FAQs on the TechNet Wiki: http://social.technet.microsoft.com/wiki/contents/articles/3919.data-quality-services-dqs-faq.aspx#Q_12_Will_DQS_provide_public_APIs.
Thanks,
Vivek
I have two website consider it as website1 and website2.
In website2 there is a login page .When a user click on the login button it will call a HTTPhandler in website1 to authenticate user.On successful authentication user information will be stored in a Session variable from handler.
Then it will redirect to a page page1.aspx in website1.But the previously set session is not available in the page1.aspx .What will be the issue?
I checked the session id in first request(when calling handler in website 1 from webiste 2) and Second request( redirecting to the page1.aspx from the handler) the session id is different.
How can i retain the session data?
You need to store session data in another process shared to both web site.
You can do it intwo different ways:
Configure an SQL server
Configure SessionState service, a Windows service used to share informations.
In both cases you have to change both web.config files to support the new session mode.
I.e. to use SQL:
Prepare a database (from command prompt):
cd \Windows\Microsoft.NET\Framework\v4.0.30319
aspnet_regsql.exe -ssadd -E -S localhost\sqlexpress
Modify web config as following:
<sessionState mode="SQLServer"
sqlConnectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Test" allowCustomSqlDatabase="true"/>
You don't need to change your code.
Correct me if I an wrong, AFAIK different domains cannot share a single session. One way to handle this is to carry the data to the other site through cookie [encrypt the values for security], then copy this cookie value to the session in the other site receiving it and destroy the cookie.
And if the sites are in different servers you need to handle the "sticky session" so that servers share the session.
This situation sounds kind of similar to one I have experienced and worked on before, where one web application acts as the login page while another is the actual app where all your work is done. I can describe what I did in the hope that you find it useful.
Like you I had one web app which had the login page (so in your example this would be website2). When the login form submitted I then redirect to a fake Login.aspx page in website1 - this is where we differ I think as I'm not sure of your specific reason for using a HttpHandler.
In my case the website2 Login.aspx page is actually just the way into the web application; it has no markup, just code-behind which will authenticate the user, perform setup (e.g. set session variables) and then redirect to another page such as Homepage.aspx. This particular scenario has worked for me, so maybe your problem revolves around the use of a HttpHandler though I would not be able to tell you why.
In order to retain the same session date across two different servers running ASP.NET web applications you must configure your session state to be managed out of process. This means the actual session state data variables will be stored outside of worker process and in another process that is able to make the session data available to other machines.
To achieve this you can configure your application to use SQL Server to store session state and make it available to multiple servers in your farm. The TechNet article Configure a SQL Server to Maintain Session State (IIS 7) provides details on hor this is done in IIS 7.
If you are using IIS 6 then the steps to configure are somewhat different and I can provide further details on this if needed.
In order for this to work you do need to ensure that both servers are running applications within the same domain, e.g. myapp.com, otherwise the ASP.Net session cookie will not be passed between the two servers. ASP.Net uses the cookie to lookup the session state stored in SQL Server and will therefore not find any matching session if the cookie is not passed on requests between the two servers.
i think IRequiresSessionState will not help because context is different.
once we had the same problem but that was passing asp session varibles to .net. How ever you can do it here also.
on both website create a page setsession.aspx
now if you are on page say web1/page5.aspx and want to go to web2/page3.aspx
you redirect to web1/setsession.aspx?togo1=web2/page3.aspx
in both setsession.aspx logic in to extract sessiondata and place them in querystring
so the web1/setsession will redirect to web2/setsession.aspx?sess1=value1&sess2=value2&togo=page3.aspx
web2/setsession.aspx will check for togo querystring and if found will extract all querystring name and value will set them in session and will then redirect to togo value.
you need to differentiate togo1 and togo carefully.
Session sharing between websites is going to require hand-coding. You could hack the asp.net framework to get this working, but I feel that this is not a clean way of achieving what you set out.
If user authentication is all you are doing from website, is it possible to use alternative? Single Sign On mechanisms will help you out here.
Something like SAMLSSO could help you in this case.
You have two websites which are hosted on different servers, it means you have two different processes running on separate machines, so sessions will be definitely different. Same session can't be shared across processes because by default asp.net support in-memory session.
Here you would need to think about storing sessions information which can be shared between two processes (i.e. out of process). Ideal way to store sessions information in databases. For this you can consider Stefano Altieri code sample above.
I don't think you really want to share session information between two websites at all. From what I can gather from comments, what you're really trying to do is have a user authenticate in one website (give you a username and password which are validated) and then have that "logged in" state transferred to another website which doesn't handle authentication for itself.
What you are describing is the Delegated Authentication model.
In this model, your application hands-off authentication to other systems which it trusts to provide information about users.
There are two well-known protocols which provide this mechanism:
OpenID
This is intended to facilitate users logging in with their own identity providers (Google, Facebook, Microsoft Account). It's a very good choice if you're running a public-facing website, as most users will already have an account they can log in with.
WS-Federation
This is intended to facilitate users logging in with identity providers which are managed by known trusted parties, such as partner organisations.
From version 4.5, the .NET Framework has built-in support for WS-Federation via the Windows Identity Foundation component (and is also available for earlier versions as a separate download). This automates the task of delegating your authentication to an Identity Provider.
It also provides components for you to write your own Identity Provider, should you want to create your own, but you shouldn't have to; you can find various existing implementations to perform this job for you.
The problem you're trying to solve is a very difficult one, especially trying to make it secure enough to be reliable. The good news is that smarter people than you or I have spent years working out very clever ways of doing this. You should use what they have done and not try to cobble together something out of Session state.
In the long-run it's best to let the smarter men do the hard work for you.