asp.net-web-api2

Redirect unauthorized requests to Azure AD for login

扶醉桌前 提交于 2019-12-24 17:30:00
问题 I've got a WebAPI instance running in Azure that is secured with Azure AD. A mobile app connects to this API using bearer tokens, works great. When I try calling the API from the browser, though, it returns a 401 because I'm not logged in. That's true because I'm not presented with a login screen. My API doesn't have any UI so what I'd want it to do is to forward the user to Azure AD login and return to the API endpoint they were calling after authentication. If I go to the Azure portal,

Azure hosted api bearer tokens wont decrypt with context.DeserializeTicket?

我怕爱的太早我们不能终老 提交于 2019-12-24 17:29:09
问题 I have 2 webapi2 projects that use the same database. If I'm on localhost I get a bearer token from one I can pass it to the other and the other will properly decrypt it and load the user. However when I publish them to separate urls on azure, I get the one token fine from one but when I pass it to the other I can't get the ticket to load and it gives me the unauthorized response. I'm testing this by watching the owin AuthenticationTokenProvider.OnRecieve method. When context

MapHttpRoute and Swagger

久未见 提交于 2019-12-24 17:19:47
问题 I'm trying to limit what shows up on swagger by limiting the routes available. Here are my current routes: config.Routes.MapHttpRoute( name: "SiteCache", routeTemplate: "{controller}/{id}/{action}", defaults: new { controller = "Sites", action ="Cache" }, constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Delete), controller = "Sites" } ); config.Routes.MapHttpRoute( name: "SitePost", routeTemplate: "{controller}", defaults: new { controller = "Sites", action ="Post" },

Modify the behaviour of JSON.NET to serialize a collection of objects to an array of IDs

谁说我不能喝 提交于 2019-12-24 16:31:44
问题 I want to modify JSON.NET so that when I am serializing a Model from my API it sends only an array of IDs for a composite Collection object. For example: class Employee { public ICollection<Address> Addresses { get; set; } } class Address { public int id; public string location; public string postcode; } Then when I send that back through WebApi Request.Createresponse(HttpStatusCode.OK, new Employee()); Instead of this: { "Addresses" : [ {"id" : 1, "location" : "XX", "postcode" : "XX" }, {"id

Programatically turn on properties serialization on .NET EF Code First

半腔热情 提交于 2019-12-24 16:22:23
问题 I have my data modeled with CodeFirst on EF 6. I'm building a Web API to which different type of clients would have access but depending on the client´s configuration they should see or not certain properties of the models. ¿How do I turn on or off the [JsonIgnore] or [serialized] ? Is it possible to set a certain set of rules to do this, like a validator? 回答1: Option 1: Using a custom ContractResolver You can create a custom contract resolver and use it when creating the response: public

Validating parameters in ASP.NET Web API model binder

↘锁芯ラ 提交于 2019-12-24 16:05:05
问题 I have a custom IModelBinder for my model: public class MyBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { // bla-bla-bla bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Request value is invalid."); return false; } } I expect that when invalid value is passed in a request HTTP 400 Bad Request is returned automatically. However, this does not happen. What should I do to make Web API return HTTP 400 if there

Where to filter Identity 2.0 claim ticket in a WebAPI app?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 15:42:44
问题 ASP.NET apps using OWIN permit multiple Identity sources (Facebook, Google, etc.). Most of the provider-specifc information those sources provide is irrelevant to my app, potentially even large, and I don't want it in my cookies all session. My app is primarily WebAPI, but I suspect the question applies equally to MVC and WebForms. For now, all I need is an integer account ID. Where/when should I reconstruct the identity, after external authentication? For example, here is one way I could

Internal Server Error api/account/register

无人久伴 提交于 2019-12-24 11:36:31
问题 I am getting an internal server error when posting information back to register an individual account within Web API 2. It happens on the client.PostAsync . The api/account/register is not changed from what was created when creating teh Web API 2 project to use individual accounts. var handler = new HttpClientHandler(); handler.UseDefaultCredentials = true; handler.PreAuthenticate = true; handler.ClientCertificateOptions = ClientCertificateOption.Automatic; handler.Credentials = new

Common code for dependency injection for web api, admin panel and mvc controller

此生再无相见时 提交于 2019-12-24 10:08:52
问题 Currently, I have below 4 projects in my solution file : API (Web API) Web (MVC) Admin (MVC) Service Layer (C# Library) The service layer is being used by all the 3 web projects. The service is injected using Autofac container. The services are registered in each of the web projects during startup which is causing duplication of the code. Is there a simpler way where I can register all the dependencies in one place so that it can be reused by all the projects? Any help is highly appreciated.

access httpcontext.session in GrantResourceOwnerCredentials

◇◆丶佛笑我妖孽 提交于 2019-12-24 09:48:06
问题 I need to get HttpContext.Session in GrantResourceOwnerCredentials method. However I get null when I try to access Httpcontext.Session . Below is my code: public void ConfigureAuth(IAppBuilder app) { PublicClientId = "self"; OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider( PublicClientId, DependencyResolver.Current.GetService<ApplicationUserManager>(), HttpContext.Current), //AuthorizeEndpointPath = new