-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Got .NET localization working in Example1
- Loading branch information
Showing
10 changed files
with
178 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Example1.RazorPages.IndividualAccounts/Pages/Localization/SetCulture.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@page | ||
@model SetCultureModel | ||
@{ | ||
} | ||
|
||
<h3>Select the culture you want</h3> | ||
|
||
<form method="post"> | ||
<div class="col-md-4"> | ||
<select name="SetLanguage" asp-items="Model.CultureList"></select> | ||
</div> | ||
<br/> | ||
<div class="form-group"> | ||
<input type="submit" value="Update" class="btn btn-primary"/> | ||
</div> | ||
</form> |
43 changes: 43 additions & 0 deletions
43
Example1.RazorPages.IndividualAccounts/Pages/Localization/SetCulture.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Localization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts.Pages.Localization; | ||
|
||
public class SetCultureModel : PageModel | ||
{ | ||
private readonly IOptions<RequestLocalizationOptions> _locOptions; | ||
|
||
public SetCultureModel(IOptions<RequestLocalizationOptions> locOptions) | ||
{ | ||
_locOptions = locOptions; | ||
} | ||
|
||
[BindProperty] public List<SelectListItem> CultureList { get; set; } | ||
[BindProperty] public string SetLanguage { get; set; } | ||
|
||
public void OnGet() | ||
{ | ||
CultureList = _locOptions.Value.SupportedUICultures | ||
.Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName }) | ||
.ToList(); | ||
} | ||
|
||
public IActionResult OnPost() | ||
{ | ||
Response.Cookies.Append( | ||
CookieRequestCultureProvider.DefaultCookieName, | ||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(SetLanguage)) | ||
); | ||
|
||
return RedirectToPage("/Index"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ public static class AppAuthSetupData | |
public static readonly List<BulkLoadUserWithRolesTenant> UsersWithRolesDefinition = new() | ||
{ | ||
new ("[email protected]", null, "Role1"), | ||
new ("Mananger@g1.com", null, "Role2"), | ||
new ("Manager@g1.com", null, "Role2"), | ||
new ( "[email protected]", null, "SuperAdmin"), | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,111 @@ | ||
using Example1.RazorPages.IndividualAccounts.PermissionsCode; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Localization; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using AuthPermissions; | ||
using AuthPermissions.AspNetCore; | ||
using AuthPermissions.AspNetCore.Services; | ||
using AuthPermissions.AspNetCore.StartupServices; | ||
using Example1.RazorPages.IndividualAccounts.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
using RunMethodsSequentially; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Example1.RazorPages.IndividualAccounts | ||
namespace Example1.RazorPages.IndividualAccounts; | ||
|
||
public class Program | ||
{ | ||
public class Program | ||
public static void Main(string[] args) | ||
{ | ||
public static void Main(string[] args) | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddDatabaseDeveloperPageExceptionFilter(); | ||
|
||
builder.Services.AddDbContext<ApplicationDbContext>(opt => | ||
opt.UseInMemoryDatabase(nameof(ApplicationDbContext))); | ||
|
||
builder.Services.AddDefaultIdentity<IdentityUser>( | ||
options => options.SignIn.RequireConfirmedAccount = false) | ||
.AddEntityFrameworkStores<ApplicationDbContext>(); | ||
|
||
//Example of configure a page as only shown if you log in | ||
builder.Services.AddRazorPages(options => | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
options.Conventions.AuthorizePage("/AuthBuiltIn/LoggedInConfigure"); | ||
}) | ||
#region localization | ||
.AddViewLocalization(options => options.ResourcesPath = "Resources"); | ||
#endregion | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureWebHostDefaults(webBuilder => | ||
#region localization | ||
builder.Services.Configure<RequestLocalizationOptions>(options => { | ||
List<CultureInfo> supportedCultures = new List<CultureInfo> | ||
{ | ||
webBuilder.UseStartup<Startup>(); | ||
}); | ||
new ("en"), | ||
new ("fr"), | ||
}; | ||
options.DefaultRequestCulture = new RequestCulture("en"); | ||
options.SupportedCultures = supportedCultures; | ||
options.SupportedUICultures = supportedCultures; | ||
|
||
options.RequestCultureProviders = new List<IRequestCultureProvider>() | ||
{ | ||
new CookieRequestCultureProvider(), | ||
//new AcceptLanguageHeaderRequestCultureProvider(), | ||
//new QueryStringRequestCultureProvider() | ||
}; | ||
}); | ||
#endregion | ||
|
||
builder.Services.RegisterAuthPermissions<Example1Permissions>() | ||
.UsingInMemoryDatabase() | ||
.IndividualAccountsAuthentication() | ||
.AddRolesPermissionsIfEmpty(AppAuthSetupData.RolesDefinition) | ||
.AddAuthUsersIfEmpty(AppAuthSetupData.UsersWithRolesDefinition) | ||
.RegisterAuthenticationProviderReader<SyncIndividualAccountUsers>() | ||
.RegisterFindUserInfoService<IndividualAccountUserLookup>() | ||
.AddSuperUserToIndividualAccounts() | ||
.SetupAspNetCoreAndDatabase(options => | ||
{ | ||
//Migrate individual account database | ||
options.RegisterServiceToRunInJob<StartupServiceMigrateAnyDbContext<ApplicationDbContext>>(); | ||
//Add demo users to the database | ||
options.RegisterServiceToRunInJob<StartupServicesIndividualAccountsAddDemoUsers>(); | ||
}); | ||
|
||
|
||
var app = builder.Build(); | ||
|
||
#region localization | ||
|
||
var options = app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value; | ||
app.UseRequestLocalization(options); | ||
|
||
#endregion | ||
|
||
// Configure the HTTP request pipeline. | ||
if (!app.Environment.IsDevelopment()) | ||
{ | ||
app.UseExceptionHandler("/Error"); | ||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapRazorPages(); | ||
|
||
app.Run(); | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-Example1.RazorPages.IndividualAccounts;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
//Example1 uses Sqlite in-memory database | ||
}, | ||
"Logging": { | ||
"LogLevel": { | ||
|
@@ -15,5 +15,5 @@ | |
"Email": "[email protected]", | ||
"Password": "[email protected]" | ||
}, | ||
"DemoUsers": "[email protected],[email protected],Mananger@g1.com" | ||
"DemoUsers": "[email protected],[email protected],Manager@g1.com" | ||
} |