简体   繁体   中英

Change HTTP response in ASP.NET WebApi

I am trying to implement server for issuing web tokens using ASP.NET WebApi. I've found great tutorial on how to do this step by step, and everything is working fine, except I need to change some values in HTTP response.

This is a picture of a call where user credentials are checked and if user exists in database he gets token, if he doesn't he gets HTTP 401 Error.

在此处输入图片说明

Here is also closer picture of body of response, cause it is too small on previous picture:

在此处输入图片说明

My question is: Is there any chance to change name of access_token property inside returned JSON to auth_token. I need to use different name because, later in the project I will have one more token and it's name has to be access_token too. My second question is: Can I changed date format for .issued and .expires properties? For example to yyyy-MM-dd?

I've used this tutorial http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

So the code is exact same. I am trying to track creation of response with debugger, but I don't have clear vision where it could be. I guess it is inside of SimpleAuthorizationServerProvider class, but I can't find explicit setup of this properties anywhere in project, so I guess that response creates automatically.

While changing the name or the format of the standard token response parameters is definitely not a good idea , here's a way to do it anyway: https://stackoverflow.com/a/28683971/542757

Basically, you'll have to use the TokenEndpointResponse notification and provide your own JSON payload. Sadly, there's a bug that prevents it from working with the OAuth2 authorization server built in Katana 3. You can take a look at a fork I developed with @manfredsteyer ; it includes a fix that allows this kind of scenario: https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM