asp.net-core-webapi

How to create a custom Authorize attribute for multiple policies in ASP.NET CORE

南楼画角 提交于 2019-12-25 00:45:26
问题 I want to authorize an action controller could access by multiple policies. .e.g: [Authorize([Policies.ManageAllCalculationPolicy,Policies.ManageAllPriceListPolicy]] public async Task<IActionResult> Get(int id){} Thank a lot. 回答1: For multiple policys, you could implement your own AuthorizeAttribute. AuthorizeMultiplePolicyAttribute public class AuthorizeMultiplePolicyAttribute:TypeFilterAttribute { public AuthorizeMultiplePolicyAttribute(string policies,bool IsAll):base(typeof

Use Windows Auth to authenticate user and then generate JWT in ASPNET Core 2.1

不羁的心 提交于 2019-12-25 00:09:02
问题 I have an ASP.NET Core 2.1 WebApi, in which I have implemented JWT authentication. The user calls api/authentication/authenticate , passes their username/password in the message body, and gets back a JWT in return which they then use to access the service. I also need the API to accept Windows authentication -- the user will call api/authentication/windows passing no user information, the service will check they are in the list of authorized users as listed in the web.config file (if I am

Upload photos in wwroot and do not load their URLs

坚强是说给别人听的谎言 提交于 2019-12-24 20:14:08
问题 I used this command to create a folder for a photo upload location. public class ApplicationRootSiteData : IApplicationRootSiteData { private readonly IHostingEnvironment _hostingEnvironment; private readonly IHttpContextAccessor _httpContextAccessor; public ApplicationRootSiteData(IHostingEnvironment hostingEnvironment, IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; _hostingEnvironment = hostingEnvironment; } public string RootFolderName => "Upload";

WebAPI Is it possible to post an IFormCollection and Object in an action?

醉酒当歌 提交于 2019-12-24 19:32:00
问题 Is it possible to post an IFormCollection and Object in an action? public Task CreateEMail(IFormCollection collection, [FromBody] Model model) I try to make an post request to WebAPI from Angular and the request is a combination of uploaded files and a model. Angular code: let model = {title:'test', subject:'test'}; let formData = new FormData(); for (let i = 0; i < this.droppedFilesData.length; i++) { let file = this.droppedFilesData[i]; let fileName = file.name; formData.append(fileName,

How to use basic authentication in my application?

笑着哭i 提交于 2019-12-24 11:00:10
问题 How to use basic authentication in asp.net core api? I have below asp.net web api controller. how to use the middleware for authentication or any other method to achieve the basic authentication in asp.net core web api. namespace Test.Web.Controllers { [Route("api/[controller]")] public class TestAPIController : Controller { // GET: api/<controller> [HttpGet] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/<controller>/5 [HttpGet("{id}")] public

C# .net core web api post parameter always null

橙三吉。 提交于 2019-12-24 09:02:28
问题 So this is driving me nuts. I'm doing something very simple sending POST request to my web api. The endpoint is set up as follows: [HttpPost] [Route("locations")] public async Task<IActionResult> PostLocations([FromBody]IEnumerable<Location>locations) and I'm making the call as follows: http://localhost:60254/api/Fetch/locations With the body { "Locations": [ { "LocationId": "111", "ProductId": 110, "Sku": "11131-LJK" } ] } And header: content-type: application/json now again, this is VERY

asp.net core web api center routing

流过昼夜 提交于 2019-12-24 07:39:06
问题 I have an issue related with asp.net core center routing . I know we could use Attribute Routing, but I did not find anything related with center routing like asp.net web api. Something like below: routes.MapHttpRoute( name: "API Default", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); Could you share me how to achieve above function in asp.net core? If there is no built-in function, could custom routing service achieve this? Regards, Edward 回答1:

Using @addTagHelper in ASP.NET Core WebAPI. ASP.NET Prerendering

一笑奈何 提交于 2019-12-24 07:26:04
问题 I am developing "ASP.NET Core Web API" with "Angular 4" . And this is my usual approach to use "Web API" template, so there are no folders such as Views . It was not a problem till I wanted to use Microsoft.AspNetCore.SpaServices . I am following this tutorial of Microsoft.AspNetCore.SpaServices Server Side Prerendering and there are two important steps to use power Microsoft.AspNetCore.SpaServices : go to your Views/_ViewImports.cshtml file, and add the following line: @addTagHelper "*,

ASP.Net core web API encode string to base64

♀尐吖头ヾ 提交于 2019-12-24 06:12:56
问题 I am new to .Net Core development. I have a model: public class CoreGoal { [Key] public long CoreGoalId { get; set; } public string Title { get; set; } public string Effect { get; set; } public string Target_Audience { get; set; } public string Infrastructure { get; set; } public virtual ICollection<Image> Images { get; set; } public CoreGoal() { } } And Image model is as following: public class Image { [Key] public long ImagelId { get; set; } public string Base64 { get; set; } [ForeignKey(

ASP.Net core web API encode string to base64

笑着哭i 提交于 2019-12-24 06:12:41
问题 I am new to .Net Core development. I have a model: public class CoreGoal { [Key] public long CoreGoalId { get; set; } public string Title { get; set; } public string Effect { get; set; } public string Target_Audience { get; set; } public string Infrastructure { get; set; } public virtual ICollection<Image> Images { get; set; } public CoreGoal() { } } And Image model is as following: public class Image { [Key] public long ImagelId { get; set; } public string Base64 { get; set; } [ForeignKey(