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
public class Cate | |
{ | |
public int Id { get; set; } | |
public int? ParentId { get; set; } | |
public string Name { get; set; } | |
} | |
var cats = new List<Cate> | |
{ | |
new Cate { |
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
try | |
{ | |
throw new InvalidOperationException("Exception message"); | |
} | |
catch (ArgumentNullException e) | |
{ | |
_logger.LogError(e.Message, e.InnerException); | |
return View("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
var path = context.Response.HttpContext.Request.Path; | |
if (path.HasValue && path.Value.Contains("api")) | |
{ | |
context.Response.StatusCode = 500; | |
await context.Response.WriteAsync("An unexpected fault happened. Try again later."); | |
return; | |
} | |
context.Response.Redirect($"/Error/Status/{context.Response.StatusCode}"); |
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
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseBrowserLink(); | |
app.UseDeveloperExceptionPage(); | |
app.UseDatabaseErrorPage(); | |
} | |
else |
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
public static class AspNetCoreGlobalExceptionHandlerExtension | |
{ | |
public static IApplicationBuilder UseAspNetCoreExceptionHandler(this IApplicationBuilder app) | |
{ | |
var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory; | |
return app.UseExceptionHandler(HandleException(loggerFactory)); | |
} | |
public static Action<IApplicationBuilder> HandleException(ILoggerFactory loggerFactory) |
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
public static class ApiGlobalExceptionHandlerExtension | |
{ | |
public static IApplicationBuilder UseWebApiExceptionHandler(this IApplicationBuilder app) | |
{ | |
var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory; | |
return app.UseExceptionHandler(HandleApiException(loggerFactory)); | |
} | |
public static Action<IApplicationBuilder> HandleApiException(ILoggerFactory loggerFactory) |
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
public async Task<IActionResult> CallApiUsingClientCredentials() | |
{ | |
var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret"); | |
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("Api1"); | |
var client = new HttpClient(); | |
client.SetBearerToken(tokenResponse.AccessToken); | |
var content = await client.GetStringAsync("http://localhost:5001/api/resource-without-policy"); | |
return View("Json", content); |
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
public async Task<IActionResult> CallApiUsingUserAccessToken() | |
{ | |
var accessToken = await HttpContext.GetTokenAsync("access_token"); | |
var client = new HttpClient(); | |
client.SetBearerToken(accessToken); | |
var content = await client.GetStringAsync("http://localhost:5001/api/resource-with-policy"); | |
return View("Json", content); | |
} |
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
public class ProfileService : IProfileService | |
{ | |
private readonly UserManager<ApplicationUser> _userManager; | |
public ProfileService(UserManager<ApplicationUser> userManager) | |
{ | |
_userManager = userManager; | |
} | |
public Task GetProfileDataAsync(ProfileDataRequestContext context) |
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
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddDbContext<ApplicationDbContext>(options => | |
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | |
services.AddIdentity<ApplicationUser, IdentityRole>() | |
.AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddDefaultTokenProviders(); | |
// Add application services. |
NewerOlder