How to define the culture for an application domain in .NET 4.5 - appdomain

According to this page, .NET 4.5 introduces the "ability to define the culture for an application domain". This is a feature I wanted for a long time, because without it you need to set the culture explicitly for each new thread if you don't want to use the system culture.
However, when I looked through the documentation I couldn't find any relevant property or method in AppDomain or AppDomainSetup.
Did someone manage to make it work? What API did you use?

You set the culture on static properties on CultureInfo class.
Have a look at CultureInfo.DefaultThreadCurrentCulture and
CultureInfo.DefaultThreadCurrentUICulture

Related

ASP.NET Core API with localized strings

We have an ASP.NET Core API. There are no views as again this is a simple API. We have strings in resource files(by culture) and we want each user to get the correct resource (in each API request we have a language name parameter). Also we like to use strongly typed resources in the code. What is the best way to do it? if i simply set the thread UI culture to be the culture from the user it works fine until there is an 'await' in the code(i assume that a different thread kicks in)

How to access ASP Core HttpContext in ASP 4.5 class library project?

I have ASP 4.5 website with a dependency on the business logic project, which is a class library built using .Net v4.5. Recently to expand the development, I have planned to introduce additional website project which is ASP Core. For the same, I have added the dependency of the business logic project. The business logic currently evaluates the request, cookies and session related stuff through HttpContext.Current instance. Which isn't working when I am accessing the business logic through ASP Core website.
Access HttpContext.Current
I have gone through the above question, and can know that why HttpContext.Current appears as null when I access it through ASP Core. And the answer to it suggests to populate the reference type IHttpContextAccessor through dependency injection. Now, the problem is, this interface belongs to the library Microsoft.AspNet.Http, and I don't see a way to add this to my business logic project.
Someone please help me out to access HttpContext into my business logic project.
I had this exact same need. The way I solved it was to create my own interfaces that live in my class library and I use those to get access to the current HttpContext regardless of whether the library is running under a 4.5 http context or a MVC Core http context.
To explain further, both the ASP 4.5 framework and the ASP Core Framework have an HttpContext object with associated Request and Response objects but they are defined in different namespaces and neither framework knows about the other framework's namespace. So What I decided is that I needed my library code to have access to an IHttpContext that was defined in one of my namespaces. And that IHttpContext would use an IHttpRequest and IHttpResponse that were also defined in my library's namespace. And finally, that IHttpRequest uses ISession, IHeaders and ICookies that are defined in my namespace.
With these interfaces defined, in my ASP 4.5 website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the ASP 4.5 HttpContext object. My HttpContext object could then be passed into my library for use.
And in my MVC Core website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the MVC Core HttpContext object. My HttpContext object could then be passed into my library for use just like it was when I was running under the ASP 4.5 environment.
So in the end, my library doesn't know which HttpContext object (4.5 or Core) it is actually accessing under the hood because it just knows that the object is has access to confirms to the IHttpInterface defined in it's library.
One final note, to help navigate name conflicts, I actually named my interfaces this way:
IAppHttpContext
IAppHttpRequest
IAppHttpResponse
IAppHttpSession
IAppHttpRequestHeaders
IAppHttpRequestCookies
Note that not all the functionality that is available in HttpContext 4.5 is available in MVC Core. The two are very similar but there are a few differences. The biggest difference is that MVC Core Session can only store byte arrays or strings whereas 4.5 session can store objects. So my IAppSession only supports storing byte arrays and strings and I have to make sure all my library's session needs work with that (all the objects that I need to store in session need to be serializable).
As you can imagine, implementing this is a bit of work, but in the end you will have a library that can access HttpContext and not care if it's running under a 4.5 HttpContext or a MVC Core HttpContext. Kinda neat.
Good luck.
i too had this same problem. I solved it by adding following dependency in my project.json file to add the http packages to class library
"Microsoft.AspNetCore.Http.Abstractions": "1.1.0"
then i used it like below
public class sampleclass
{
private readonly IHttpContextAccessor context;
public ISession GetSession()
{
return context.HttpContext.Session;
}
}
Thank you. Happy Coding :-)
Reference : http://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/

What is equivalent in .net core for context.environment.add?

In a .net core app when you want to set a property that is available in all events, what is the same as context.environment.add in previous versions of .net?
Assuming you are talking about Owin middleware, the context passed in to IApplicationBuilder.Use is of type HttpContext which has an Items property which is a dictionary with object keys and values.

Custom ClaimsPrincipal in Asp.Net 5 (Mvc 6)

How can I override the default Context.User of type System.Security.Claims.ClaimsPrincipal in Asp.Net 5 (MVC-6)?
I would like to use my customized User-type, so that it's accessible in Controllers (HttpContext.User), as well as in the Razor views (#User.)
Any help is much appreciated. Thanks!:)
The basic answer is don't - it's generally a bad idea to try to implement your own security code; there's lots of options out there that will save you a lot of time up front, and save you a lot of headaches on the back side, too.
The other answer is that you can't - it's built in to the new framework from the beginning.
This is what a User POCO model is for. The Identity framework has operated this way (I think since the beginning), and it mirrors OAuth and most other authentication/authorization systems. It's an incredibly flexible and efficient model.
Instead what I would recommend doing is build your own ClaimTypes and use those in addition to the ones built in to the framework. Depending on how you're authenticating the user, you should be able to add them when you would create the IPrincipal, anyway.
Short answer - assign custom .User to HttpContext in middleware.
Long answer: Where to set custom ClaimsPrincipal for all HttpRequests

WCF Service Passing TimeZoneInfo

I have a Silverlight App that calls my WCF service to get a list of the Time zones from the server. All Time zones are retrieved in the function on the server but I need to know how to pass these back to Silverlight.
My call on the server is below but I think I need to somehow serialize the TimeZoneInfo as a Know Type before I can pass it back. This is the point where I am stuck.
Please can someone help on this?
Public Function GetTimezones() As ReadOnlyCollection(Of TimeZoneInfo) Implements ITimezoneService.GetTimezones
Dim timeZones As ReadOnlyCollection(Of TimeZoneInfo) = TimeZoneInfo.GetSystemTimeZones()
Return timeZones
End Function
See this http://social.msdn.microsoft.com/Forums/en/wcf/thread/f164f185-ae18-4775-a2ff-a814813d262d for a list of known types to add to allow TimeZoneInfo to be serialized.
This will work for the full framework but I don't really recommended it, I would rather use either the ID, or the built-in string serialization as proposed in c# TimeZoneInfo serialization.
On the contrary, the Silverlight framework has its own lightweight version of TimeZoneInfo and thus won't be able to deserialize the TimeZoneInfo serialized by the full framework (since it's not the same type definition).
You should also note that, by default security parameter Silverlight has only access to UTC and Local (Silverlight client running machine locale) TimeZoneInfo, and that to use other time zones, you need to run Silverlight with elevated privileges because timezone information is registry based. See silverlight Time Zone converting and http://forums.silverlight.net/t/186363.aspx/1.
In the end some did rewrite whole or part of the TimeZoneInfo class in their own Silverlight application (we had to too). See http://forums.silverlight.net/t/165067.aspx/1.
Silverlight doesn't have yet support for more than local to UTC or UTC to local time zone conversion natively.