asp.net-core-2.0

How to get windows user name when enabling both windows authentication and anonymous authentication

杀马特。学长 韩版系。学妹 提交于 2019-12-05 18:27:37
I've built my api in .net core for the first time in core 2.0. The client is built using vs 2017 angular template. My api is used even by other applications which may not be using windows authentication. For those functions I want to allow anonymous access. For this reason I've to enable both windows authentication and anonymous authentication. But when enable both I know I cannot get windows user name. In that case how can get the windows user name? The following code breaks when I enable anonymous authentication along with windows authentication. [Route("current")] public ADUser

Is services.AddSingleton<IConfiguration> really needed in .net core 2 API

岁酱吖の 提交于 2019-12-05 15:19:21
I accessed appsettings.json In .NET Core 2 Web API Controller simply by adding below: public class MyController : Controller { private readonly IConfiguration appConfig; public MyController(IConfiguration configuration) { appConfig = configuration; } } Without adding below in Startup class ConfigureServices(IServiceCollection services) after services.AddMvc();: services.AddSingleton<IConfiguration>(Configuration); Is there any flaws in my approach? In official docs for .Net Core 2 configuration section, its not mentioned to use 'AddSingleton' not even once: https://docs.microsoft.com/en-us

How to avoid n+1 queries in EF Core 2.1?

爱⌒轻易说出口 提交于 2019-12-05 13:11:56
I'm using EF Core 2.1 preview which was supposed to reduce N+1 queries problem. I'm trying to make query, that selects Forum Threads with authors of posts: dbContext.ForumThreads .Include(t => t.Posts) .Take(n) .Select(t => new { t.Id, t.Title, PostAuhtors = t.Posts.Select(p => p.Author).Take(5) }).ToArray(); This produces n+1 queries: For each ForumThread it selects post authors The schema is simple: public class ForumThread { public Guid Id {get;set;} public string Title {get;set;} public ICollection<ForumPost> Posts {get;set;} } public class ForumPost { public Guid Id {get;set;} public

How to use ASP.NET Core Identity without roles?

十年热恋 提交于 2019-12-05 12:26:48
Is it feasible to implement identity in asp.net core 2 without roles implementation? I have tried to implement the following: services.AddIdentityCore<TUser>(); but that does not seem to work as well. I got it! I have upload a repo in github: https://github.com/tolemac/IdentityWithoutRoles You have to create your custom ApplicationDbContext with corrects DbSets : public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions options) : base(options) { } /// <summary> /// Gets or sets the <see cref="DbSet{TEntity}"/> of Users. /// </summary> public DbSet

Asp.net core 2.0 middleware - accessing config settings

﹥>﹥吖頭↗ 提交于 2019-12-05 11:51:42
In a asp.net core web api middleware, is it possible to access the configuration inside the middleware? Can someone guide me how this is done? In a middle-ware you can access settings. To achieve this, you need to get IOptions<AppSettings> in the middle-ware constructor. See following sample. public static class HelloWorldMiddlewareExtensions { public static IApplicationBuilder UseHelloWorld( this IApplicationBuilder builder) { return builder.UseMiddleware<HelloWorldMiddleware>(); } } public class HelloWorldMiddleware { private readonly RequestDelegate _next; private readonly AppSettings

Asp.Net Core middleware pathstring startswithsegments issue

こ雲淡風輕ζ 提交于 2019-12-05 10:54:52
Have a Asp.NET Core 2.0 application and I would like to map any path that does not start with /api to just reexecute to the root path. I added the below but doesn't seem to work: app.MapWhen( c => !c.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase), a => a.UseStatusCodePagesWithReExecute("/") ); Not using MapWhen() and just using app.UseStatusCodePagesWithReExecute("/") works for all paths not root. Just want to add filtering for all paths not root and not /api . Any ideas on how to do this? Branched pipeline does not work correctly here because you have not added

.Net Core IdentityServer4 Get Authenticated User

会有一股神秘感。 提交于 2019-12-05 09:49:35
I'm trying to figure out how to retrieve a logged in user from Identity server 4 using .Net-Core 2. My authentication is working currently, I'm just trying to figure out how I can retrieve the claims Identity from the HTTP Context. services.AddAuthentication(options => { options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; }).AddIdentityServerAuthentication(o => { o.Authority = IDP_AUTHORITY_URL; o.RequireHttpsMetadata = false; o.ApiName = API_ID; o

The type ApplicationUser cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser>'

空扰寡人 提交于 2019-12-05 08:41:19
Trying to implement Identity in ASP.NET Core 2.0. Having many problems getting my head around this. Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Ctrack6_Custom")) ); services.AddIdentity<ApplicationUser, ApplicationRole>(

Can you set request timeout in asp.net core 2.0 hosted in IIS from C# code?

喜你入骨 提交于 2019-12-05 08:09:43
Is there a way to set requestTimeout from C# instead of needing to set requestTimeout in the web.config ? asp.net core 2.0 hosted in IIS <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore requestTimeout="00:00:04" processPath="dotnet" arguments=".\Foo.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> </configuration> Not like this No, there is no way to do that as you described. But according to the

log4net AdoNetAppender in .Net core 2.0 not supported?

天大地大妈咪最大 提交于 2019-12-05 08:06:51
I'm implementing log4net AdoNetAppender in asp.net core 2.0 , but I guess it is not supporting. I have implemented log4net RollingFileAppender in core 2.0 & it worked successfully using log4.net config. So, if log4net AdoNetAppender is not supporting in core 2.0, is there any other way to insert logs to sql database in core 2.0? Thank you I faced the same issue and solved using this nuget package in .net core solution https://www.nuget.org/packages/MicroKnights.Log4NetAdoNetAppender You can find more information about how to set this up on https://github.com/microknights/Log4NetAdoNetAppender