I am trying to create a web API
with form based authentication. I want to log in with a customer and recover data from there. When I log in, the user is authenticated and can receive data directly in the address bar such as http request
such as localhost: 1393 / api / game
. But when I try to get it from the client, I'm getting a 401 (unauthorized error)
. I have enabled the Corus in the server side, this is the controller to handle the data
using the system; Using System.Collections.Generic; Using System.Data; Using System.Data.Entity.Infrastructure; Using System.Linq; Using System.Net; Using System.Net.Http; Using System.Web; Using System.Web.Http; Using System.Web.Security; Using cheeky_backend.models; Using System.Web.Http.WebHost; Namespace Cheeky_backend.Controllers {Public Class Demo {Public List & amp; TT; Teams & gt; Team {Receive; Set; } Public list & lieutenant; Hole & gt; Go to the hole; Set; }} [Authorized] Public Classroom Game Controller: API Controller {Private Check_backed Contains DB = New Cheek_Babend Contex (); // GET API / Game Public Enemereble & lt; Hole & gt; GetHoles () {return db.Holes.Asumerable (); }}}
This is the authentication controller
using the system; Using System.Collections.Generic; Using System.Data; Using System.Data.Entity.Infrastructure; Using System.Linq; Using System.Net; Using System.Net.Http; Using System.Web; Using System.Web.Security; Using System.Web.Http; Using cheeky_backend.models; Namespace Cheeky_backend.Controllers {Public class UserController: ApiController {Private Cheeky_backendContext DB = New Cheeky_backendContext (); // GET api / Default1 // GET api / Default1 / 5 // PUT API / Default 1/5 // Post API / Default 1 Public HTTPPS End User (User User) {if (ModelState.IsValid) {db.Users. Add (user); Db.SaveChanges (); HttpResponseMessage Response = Request CreateResponse (HTTPTat code, ready, user); // response.Headers.Location = New URI (Url.Link ("DefaultApi", new {id = user.ID})); Return feedback; } And {return request. Create Response (HTTP status code. Badrequest); }} // DELETE api / Default1 / 5 public HTTPPPS password (user user) {var userfound = user2 to db.uppress where user.usem == user 2.usem end & amp; Choose user.password == user2.password user2; If (userfound.Any ()) {FormsAuthentication.SetAuthCookie (user.username, true); Return request. CreteSpons (HTTTP code.OK, user); } Return request. CreteSpons (HTTTPTas code.authorized); }}
In your authentication handler < Do not set any principal on / strong>
-
thread.
Take a look at html>
Comments
Post a Comment