Localize ASP.NET Identity error messages [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In my account controller I have something like this:
var result = await UserManager.CreateAsync(user, model.Password);
foreach (var error in result.Errors)
{
modelstateErrors.Add(error);
}
Every error string is localized in English language
What's the best practice in localizing ASP.NET Identity error messages?
Are there any libraries with localized errors, and how are they implemented?
Would it be good idea to switch on every ASP.NET Identity error and return your own localized string?

To localize ASP.Net Identity you need to install one of the following Nuget packages from the Nuget store => https://www.nuget.org/packages?q=Microsoft.AspNet.Identity.Core
You install the package that belong to your culture. So for French culture you should install Microsoft.AspNet.Identity.Core.fr
They all follow the pattern Microsoft.AspNet.Identity.Core.[Culture] where [Culture] is the code fo the culture.

Create e base controller and extend every controller from it
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
// verify which kind of exception it is and do somethig like logging
}
}
It is one of the best practice por handlling errors, but for the localizing itself do what #codeNotFound said.

Related

How to add AppInsights into Class Library [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 days ago.
The community is reviewing whether to reopen this question as of 3 days ago.
Improve this question
I went through the following tutorial to add AppInsights into a C# project
https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net
Using the manual method. All worked as expected.
I now want to add this same feature into a .Net Framework Class Library.
Following that tutorial I can't carry out steps 5 and 6 because they are explicitly used in an MVC project.
How could I add or what is the equivalent code for steps 5 and 6 from that link to add the same code into a .Net Framework Class Library?
Edit 1
So after implementing the manual method in an MVC app all looks good.
In my Class Library in the constructor i have added similar code as below
private TelemetryClient _telClient;
public class SomeClass(TelemetryClient telClient)
{
_telClient = new TelemetryClient();
}
public void SomeMethod()
{
_telClient.TrackException(new Exception("Hello World");
}
In my MVC app i have the below code
if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
{
//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
var ai = new TelemetryClient();
ai.TrackException(filterContext.Exception);
SomeClass sc = new SomeClass(ai);
}
}
For a .Net Framework Class library you should only expect a TelemetryClient (or a TelemetryConfiguration so you can create a TelemetryClient) to be passed to the constructor using a constructor argument or dependency injection.
Typically the reading the ApplicationInsights.config file and constructing a client is done by the code that calls your class library. You shouldn't do that in the class library itself.
Then, in your class library you can manually call the methods of the TelemetryClient like TrackEvent(..), TrackException(...), TrackDependency(...) etc.
Steps 5 and 6 take care of tracking unhandled exceptions on a controller level. You cannot do that in a class library unless you want to provide an exception handler to the calling code.
So, unless you want to manually send telemetry from within your class library you shouldn't bothered about Application Insights at all in your class library.

Fluent validation for httpget request webapi core 3.0 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
public async Task<ActionResult> GetCustomerById(int id)
{
var data = await _customerService.GetCustomerById(id);
return Ok(data);
}
how can we implement fluent validation for request parameter - id?
You can use it like this :
RuleFor(id => id).LessThan(100).GreaterThan(0);
You don't need FluentValidtion to validate the request, It's an Attribute Routing. It helps you to not process invalid request.
[HttpGet("GetCustomerById/{id:int:min(1)}")]
public async Task<ActionResult> GetCustomerById(int id)
{
}

Can we control the [Setup] method execution before [Test] Method Execution in Selenium N unit [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Am just wondering how to solve this. I need to automate my company website. There I need to navigate more than one url for a multiple web pages. I have designed Hybrid framework along with Page object Model Design.
My Requirement is,
say I have 3 url's :
www.google.com
www.yahoo.com
Facebook
All the above url and its test data I will keep in an Excel sheet. I have created three different pages and three different test classes.
So my list of questions are:
How to pass url's one by one to [setup] method
how to call the test method deepening upon the url type
Execution Flow need to implement of Application:
You need to parametrize your test with TestCase attribute.
[TestCase("www.google.com")]
[TestCase("www.yahoo.com")]
[TestCase("www.facebook.com")]
public void WebPageTest(string site)
{
driver.Url(site);
//continue with the test.
}
See this article to learn more: https://github.com/nunit/docs/wiki/TestCase-Attribute
Storing URL in excel is not good idea,
You may store URL in app.config file and by using ConfigManager utility you may retrieve those URL from app.config file
As according to your test cases you can use URL where its needed and required
I would suggest you to use [category] attribute to categorise your test cases. For example
[Test]
[Category("GoogleTest")]
public void googletest1()
{
}
[Test]
[Category("FBTest")]
public void fbtest1()
{
}
Now in the [SetUp] method you can load url based on the category, something like
[SetUp]
public void testsetup()
{
#initialise driver
var category = TestContext.CurrentContext.Test.Properties.Keys;
if(category.Contains("GoogleTest"))
{
//category1 setup
}
else if(category.Contains("FBTest"))
{
//category2 setup
}
}
So using this method you can solve query # 2, i.e the url related to the test is already loaded for you, so you can continue with your tests after setup

How do i remove the '.aspx' extension from url [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How do i remove extensions from page URL in c#.
e.g: questions/ask.aspx
I want the url of my web application in following format:
questions/ask
If any one have a idea then pleas guide me...
If you are using web forms you need to add a custom router handler using URL Routing in the Global.asax file.
Check out this sample:
Global.asax
public class Global : System.Web.HttpApplication
{
//Register your routes, match a custom URL with an .aspx file.
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("About", "about", "~/about.aspx");
routes.MapPageRoute("Index", "index", "~/index.aspx");
}
//Init your new route table inside the App_Start event.
protected void Application_Start(object sender, EventArgs e)
{
this.RegisterRoutes(RouteTable.Routes);
}
}
You have to Implement URL Rewriting
URL rewriting is the process of intercepting an incoming Web request
and redirecting the request to a different resource. When performing
URL rewriting, typically the URL being requested is checked and, based
on its value, the request is redirected to a different URL
You can Add this in Web.Config
<urlMappings enabled="true">
<add url="~/questions/ask" mappedUrl="~/questions/ask.aspx?page=Ask"/>
</urlMappings>
See Here

The best way to persist a unique id on browser? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is the best way to persist a unique id on a browser session in MVC.NET?
Is there some session/cookie ID value by default?
In my Global.asax, I could create a new Session["ID"] and use its ID property.
There's got to be another way?
I tried using
var session = HttpContext.Current.Session;
UserResearch userResearch = new UserResearch();
userResearch.SessionID = sesstion.SessionID.ToString();
But i get an error:
System.NullReferenceException: Object reference not set to an instance of an object
I need to pull initial browser and clicking research based on a user that hasnt logged in to anything, so i need some way to refer to them, hence the id.
I could create a unique id on the sql end and store it in a session, just seems like there should be a more straight forward way.
Is there a browser session id?
Your problem most likely is caused by where in the code you are trying to access HttpContext, this is why your getting a NullReference for Session. But assuming you get that worked out this is how I'd approach your problem.
I would just store a GUID in a cookie, then Get/Set it like so: (Untested)
public Guid SessionGUID(){
if(HttpContext.Current.Request.Cookies["SessionGUID"])
{
//return the SessionGUID
return HttpContext.Current.Request.Cookies["SessionGUID"].value as Guid;
}
else//new visit
{
//set cookie to a new random Guid
var _guid=Guid.NewGuid();
HttpCookie guidCookie = new HttpCookie("SessionGUID");
guidCookie.Value = _guid;
guidCookie.Expires = DateTime.Now.AddDays(1d);
HttpContext.Current.Response.Cookies.Add(guidCookie);
return _guid;
}
}
As per https://stackoverflow.com/users/374310/igor: thanks!
global.asax is a file, class MvcApplication inside it may implement several methods/event handlers, some of those are called when Session is not available yet. Session_Start should work for you.
var session = HttpContext.Current.Session;
UserResearch userResearch = new UserResearch();
userResearch.SessionID = sesstion.SessionID.ToString();
I put it in Session Start and Voila!

Categories

Resources