ASP.NET MVC & WEB API token authentication

Asked

Viewed 757 times

0

I have a question about the token bearer. I have two scenarios in my application. 1º is an admin panel that works with ASP.NET MVC and Angularjs and the 2nd is the WEB Api that exchanges requests with a WPF application. My question is about the token, if it is indifferent I use it for an ASP.NET MVC controller and a web api controller.

 public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();            
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        try
        {
            DbEngine db = new DbEngine();
            var user = db.AutenticacaoAPI(context.UserName, context.Password);
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            if (user.Status)
            {
                identity.AddClaim(new Claim(ClaimTypes.Name, user.Value.ST_NOME));
                identity.AddClaim(new Claim(ClaimTypes.Email, user.Value.ST_LOGIN));
            }

            context.Validated(identity);
        }
        catch
        {
            context.SetError("Usuário e senha inválidos", "Error");
        }
    }
}

and the Controller code to request user information only to test the service

[Authorize]
    [HttpGet]
    public JsonResult UserInfo(string login)
    {
        using (DbEngine db = new DbEngine())
        {
            try
            {
                return new JsonResult
                {
                    Data = db.GetUser(login),
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet

                };
            }
            catch (Exception ex)
            {
                return ex.ReturnException(HttpStatusCode.InternalServerError);
            }
        }
    }

Here is the return of the token

inserir a descrição da imagem aqui

And here the call that returns this error even with the token in the header

inserir a descrição da imagem aqui

I appreciate the help.

1 answer

2


Your problem is not with the ASP.NET Identity or with the ASP.NET MVC, but when trying to serialize the Entity proxy Usuario.

This Proxy is created so that it is possible to perform the LazyLoad in the queries, then try to disable Lazyload and switch to using the .Include()) whenever necessary.

  • Thanks, I’ll try to deactivate to see if it works. I appreciate your help

  • I deactivated Lazy Load but continued the same problem, so I did some more research and found the solution at this link: http://stackoverflow.com/questions/5588143/ef-4-code-first-json-circular-reference-serialization-error so I just entered db.Configuration.ProxyCreationEnabled = false; and I got to do the get. I’ll mark it as the right answer because you gave me the right direction to solve my problem, I appreciate your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.