Where to put general language adjustment? - asp.net

Well, the title says it all.
I do the following now in my LanguageFilterAttribute class:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
string currentUrl = request.RawUrl;
var urlHelper = new UrlHelper(request.RequestContext);
string baseurl = urlHelper.Content("~");
string currentLanguageFromUrl = currentUrl.Split('/')[1];
string currentLanguageFromCulture = CultureHelper.CheckCulture();
var currentLanguageFromCookie = request.Cookies["_culture"];
var possibleCultures = UnitOfWork.CulturesRepository.GetListOfCultureNames();
if (possibleCultures.All(culture => currentLanguageFromUrl != culture))
{
string cultureName;
string newUrl;
if (currentLanguageFromCookie != null)
{
cultureName = currentLanguageFromCookie.Value;
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
if (currentLanguageFromCulture != null)
{
cultureName = currentLanguageFromCulture;
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
cultureName = possibleCultures[0];
CultureHelper.SetCulture(cultureName);
newUrl = baseurl + cultureName;
filterContext.Result = new RedirectResult(newUrl);
return;
}
CultureHelper.SetCulture(currentLanguageFromUrl);
base.OnActionExecuting(filterContext);
};
Which sets the language when you select a new one from the dopdown on the shared Layout page (this works btw, selecting a different language triggers respectively, the above and below class correctly).
public static void SetCulture(string culture)
{
var cultureCookie = HttpContext.Current.Request.Cookies["_culture"] ?? new HttpCookie("_culture");
cultureCookie.Value = culture;
var request = HttpContext.Current.Request;
cultureCookie.Domain = request.Url.Host;
cultureCookie.Expires = DateTime.Now.AddYears(1);
cultureCookie.Path = "/";
HttpContext.Current.Response.Cookies.Add(cultureCookie);
CultureInfo info = CultureInfo.CreateSpecificCulture(culture.ToString());
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = info;
}
The problem with this is, as you can guess, I will have to apply the [LanguageFilter] attribute on all my controllers.
Isn't there a file where I can place this that will change my language every time I go to another page?

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
When a new application thread is started, its current culture and
current UI culture are defined by the current system culture, and not
by the current thread culture.
Isn't this your case?

Related

ASP.NET WEB API cant upload multipart/form-data (image) to Web server

After uploading image through ASP.NET WEB API successfuly through localhost . I uploaded my project to the hosting server but this time i got the error as
an error has occured
This is my controller
tutorEntities entities = new tutorEntities();
[HttpPost]
public HttpResponseMessage ImageUp()
{
var httpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
img std = new img();
// std.Title = httpContext.Request.Form["Title"];
// std.RollNo = httpContext.Request.Form["RollNo"];
// std.Semester = httpContext.Request.Form["Semester"];
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
string random = Guid.NewGuid().ToString();
string url = "/UserImage/" + random + httpRequest.Files[0].FileName.Substring(httpRequest.Files[0].FileName.LastIndexOf('.'));
Console.WriteLine(url);
string path = System.Web.Hosting.HostingEnvironment.MapPath(url);
httpRequest.Files[0].SaveAs(path);
std.Path = "http://localhost:2541/" + url;
}
entities.imgs.Add(std);
entities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
Ensure that AppPoolIdentity user has write permissions on the UserImage folder. Also, catch the exception in code for debugging:
try {
var httpContext = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
img std = new img();
// std.Title = httpContext.Request.Form["Title"];
// std.RollNo = httpContext.Request.Form["RollNo"];
// std.Semester = httpContext.Request.Form["Semester"];
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
string random = Guid.NewGuid().ToString();
string url = "/UserImage/" + random + httpRequest.Files[0].FileName.Substring(httpRequest.Files[0].FileName.LastIndexOf('.'));
Console.WriteLine(url);
string path = System.Web.Hosting.HostingEnvironment.MapPath(url);
httpRequest.Files[0].SaveAs(path);
std.Path = "http://localhost:2541/" + url;
}
entities.imgs.Add(std);
entities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
catch(Exception ex) {
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(ex.ToString()),
ReasonPhrase = "Error"
}
throw new HttpResponseException(response);
}

Kentor:[KeyNotFoundException: No Idp with entity id "http://adfs.domain.com/adfs/services/trust" found.]

We have a requirement to enable SAML SSO login, we are implementing SSO using Kentor HttpModule.
I'm facing an issue when the Idp calls my application. The kentor service throws The given key was not present in the dictionary. Here the idp is ADFS.
We tried using the stubidp and it works fine.
Below is my Saml configuration
private static IdentityProvider CreateAuthServicesOptions()
{
var spOptions = GetServiceProviderOptions();
var idp = new IdentityProvider(new EntityId("http://IQTDEV01.domain.com/adfs/services/trust/"), spOptions)
{
AllowUnsolicitedAuthnResponse = true,
Binding = Saml2BindingType.HttpPost,
WantAuthnRequestsSigned=true,
//LoadMetadata = true,
SingleSignOnServiceUrl = new Uri("https://IQTDEV01.iqtrackdev.com/adfs/ls/")
};
idp.SigningKeys.AddConfiguredKey(
new X509Certificate2(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/ADFSService.cer"));
return idp;
}
private static SPOptions GetServiceProviderOptions()
{
var cultureInfo = CultureInfo.GetCultureInfo("en-US");
var spOptions = new SPOptions
{
EntityId = new EntityId("https://app.domain.com/AuthServices/"),
ReturnUrl = new Uri("https://app.domain.com"),
AuthenticateRequestSigningBehavior=SigningBehavior.Always
};
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection cers = store.Certificates.Find(X509FindType.FindByThumbprint, "‎FDDAF5EAA6E2B232E0012C0E77955C13246D2DF4", false);
Kentor.AuthServices.ServiceCertificate ser = new Kentor.AuthServices.ServiceCertificate();
ser.Certificate = cers[0];
ser.Use = Kentor.AuthServices.CertificateUse.Signing;
spOptions.ServiceCertificates.Add(ser);
//spOptions.ServiceCertificates.Add(new X509Certificate2(
// AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/ADFSService.cer"));
return spOptions;
}
protected void OnAuthenticateRequest(object sender, EventArgs e)
{
var application = (HttpApplication)sender;
// Strip the leading ~ from the AppRelative path.
var appRelativePath = application.Request.AppRelativeCurrentExecutionFilePath;
appRelativePath = (!string.IsNullOrEmpty(appRelativePath))
? appRelativePath.Substring(1)
: string.Empty;
if (application.Request != null)
{
Kentor.AuthServices.Configuration.Options op = new Options(GetServiceProviderOptions());
op.IdentityProviders.Add(CreateAuthServicesOptions());
Options = op;
}
var modulePath = Options.SPOptions.ModulePath;
if (appRelativePath.StartsWith(modulePath, StringComparison.OrdinalIgnoreCase))
{
var commandName = appRelativePath.Substring(modulePath.Length);
var command = CommandFactory.GetCommand(commandName);
var commandResult = command.Run(
new HttpRequestWrapper(application.Request).ToHttpRequestData(),
Options);
if (!commandResult.HandledResult)
{
commandResult.SignInOrOutSessionAuthenticationModule();
commandResult.Apply(new HttpResponseWrapper(application.Response));
}
}
}
Exception

filterContext for ActionResult in OnResultExecuted Method of ASP.NET MVC

I need to filter only methods from all actions which have return type ActionResult from controller actions.
I am getting controller name and action name from following..
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
but how can i filter only method which have return type ActionResult?
Try this kind of code for accessing Controllers, Actions as well as
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
string originArea = String.Empty;
if (filterContext.RouteData.DataTokens.ContainsKey("area"))
originArea = filterContext.RouteData.DataTokens["area"].ToString();
Try this in your Action Filter:
var controllerActionDescriptor = filterContext.ActionDescriptor as System.Web.Mvc.ReflectedActionDescriptor;
if (controllerActionDescriptor == null ||
controllerActionDescriptor.MethodInfo.ReturnType != typeof(ActionResult))
{
return;
}
// if we got here then Action's return type is 'ActionResult'
Update:
Since you're using the OnResultExecuted method, try this:
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
var actionType = filterContext.Controller.GetType().GetMethod(originAction).ReturnType;
if (actionType != typeof(ActionResult))
return;
// if we got here then Action's return type is 'ActionResult'
}
Update:
As per your comment, in case there is more than one Action with the same name (overloading):
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var actionName = filterContext.RouteData.Values["action"].ToString();
var ctlr = filterContext.Controller as Controller;
if (ctlr == null) return;
var invoker = ctlr.ActionInvoker as ControllerActionInvoker;
if (invoker == null) return;
var invokerType = invoker.GetType();
var getCtlrDescMethod = invokerType.GetMethod("GetControllerDescriptor", BindingFlags.NonPublic | BindingFlags.Instance);
var ctlrDesc = getCtlrDescMethod.Invoke(invoker, new object[] {ctlr.ControllerContext}) as ControllerDescriptor;
var findActionMethod = invokerType.GetMethod("FindAction", BindingFlags.NonPublic | BindingFlags.Instance);
var actionDesc = findActionMethod.Invoke(invoker, new object[] { ctlr.ControllerContext, ctlrDesc, actionName }) as ReflectedActionDescriptor;
if (actionDesc == null) return;
if (actionDesc.MethodInfo.ReturnType == typeof (ActionResult))
{
// you're in
}
}

POST data to XML service aspx vb

Dim doc As New XmlDocument
doc.Load("http://www.example.com/?paramx=1&paramy=2")
is great for GET queries in the querystring. But say I wanted to POST the paramx=1&paramy=2 to http://www.example.com and get the response in XML. How would I do that?
Below is a helper class I use for POSTing data to a website which then returns XML - hope it helps.
Usage:
var resultXmlDoc = new HttpHelper().PostXml("http://www.test.com", new { paramName = "value", paramName2="value2" });
Helper class:
internal class HttpHelper
{
public XDocument PostXml(string baseUrl, IDictionary<string, string> cgiParams)
{
return XDocument.Load(Post(baseUrl, cgiParams).GetResponseStream());
}
public XDocument PostXml(string baseUrl, params object[] items)
{
return XDocument.Load(Post(baseUrl, items).GetResponseStream());
}
public XDocument PostXml(string url, string topost)
{
return XDocument.Load(Post(url, topost).GetResponseStream());
}
protected virtual HttpWebResponse Post(string url, string postString)
{
if (url == null)
throw new ArgumentNullException("baseUrl");
Uri uri;
if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
throw new ArgumentException("url is not a valid Uri");
var req = (HttpWebRequest)WebRequest.Create(url);
// if the target site issues a session cookie, you need to store it and append it here
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
var bytes = System.Text.Encoding.ASCII.GetBytes(postString);
req.ContentLength = bytes.Length;
using(var str = req.GetRequestStream())
str.Write(bytes, 0, bytes.Length);
return (HttpWebResponse)req.GetResponse();
}
protected virtual HttpWebResponse Post(string url, params object[] items)
{
var x = items.SelectMany(i =>
{
return i.GetType().GetProperties().Select(p => new Tuple<string, string>(p.Name, p.GetValue(i, null) != null ? p.GetValue(i, null).ToString() : ""));
});
var d = new Dictionary<string, string>();
foreach (var p in x)
d[p.Item1] = p.Item2;
return Post(url, d);
}
protected virtual HttpWebResponse Post(string baseUrl, IDictionary<string, string> cgiParams)
{
if (baseUrl == null)
throw new ArgumentNullException("baseUrl");
Uri uri;
if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out uri))
throw new ArgumentException("baseUrl is not a valid Uri");
string postString = string.Empty;
if (cgiParams != null && cgiParams.Count > 0)
{
foreach (var k in cgiParams.Keys)
postString += HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(cgiParams[k]) + "&";
postString = postString.Substring(0, postString.Length - 1);
}
return Post(baseUrl, postString);
}
}

Parameter has all propertys after webrequest

I have a really simple ASP.NET Api Controller with one method.
public HttpResponseMessage<User> Post(User user)
{
return new HttpResponseMessage<User>(new User() { Name = "New User at Server" });
}
My debugger says that the method is called but the problem is that the parameter "user" has all its content set to null; I am using Fiddler to look at request and response.. and all looks good.
This is my service code in the client.
public void AddUser(Models.User user, Action<Models.User> ShowResult)
{
var uiThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();
string url = "http://localhost:4921/User";
Uri uri = new Uri(url, UriKind.Absolute);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var sendWebPost = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null)
.ContinueWith(task =>
{
Tuple<string, string>[] stringToSend = { Tuple.Create<string, string>("user", ObjectToJson<Models.User>(user)) };
var bytesToSend = GetRequestBytes(stringToSend);
using (var stream = task.Result)
stream.Write(bytesToSend, 0, bytesToSend.Length);
}
).ContinueWith(task =>
{
Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null)
.ContinueWith<WebResponse>(task2 => { ValidateResponse(task2); return task2.Result; })
.ContinueWith<Models.User>(task3 => {return JsonToObject<Models.User>(task3);})
.ContinueWith(task4 => { TryClearWorking(); ShowResult(task4.Result); }, uiThreadScheduler);
});;
}
public static string ObjectToJson<T>(T obj) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
MemoryStream stream = new MemoryStream();
StreamReader sr = new StreamReader(stream);
serializer.WriteObject(stream, obj);
sr.BaseStream.Position = 0;
string jsonString = sr.ReadToEnd();
return jsonString;
}
protected static byte[] GetRequestBytes(Tuple<string, string>[] postParameters)
{
if (postParameters == null || postParameters.Length == 0)
return new byte[0];
var sb = new StringBuilder();
foreach (var key in postParameters)
sb.Append(key.Item1 + "=" + key.Item2 + "&");
sb.Length = sb.Length - 1;
return Encoding.UTF8.GetBytes(sb.ToString());
}
Anyone who can give me some ideas where to start to look for errors.....
You need to set the Content-Length header.

Resources