use authorization token on multiple api websites - c#

I have created a secured Web API 2 website. I have used oAuth2 to authenticate requests and it works. I get an authorization token (bearer) which I can use to call data from that website. Let's call this the authenticationWebsite
However...I need to create multiple API websites. Is it possible that I authenticate myself on the authenticationWebsite where I get a authorization token and then use that token to call data from my other api websites?
I have tried a simple example and created another website (mvc) which gets the authentication token from the authenticationWebsite but when I try to get data from the new website (from a controller which has the authorize attrubute, I get the unauthorize error.
I hope the question is clear.

Ok I figured it out. It is important that the authentication server and the resource server(s) have the same Machine key in the web config.

Related

Use Azure AD B2C Cookie across ASP.Net Core Web Apps

I have two ASP.Net Core 3 Web App projects. Both use Azure AD B2C to authenticate. One is technically an API but I have included the Cookie authentication service so that the browser can easily be used to make API requests.
This seems to work although each app likes to request its own cookie which overrides the other's. I could avoid them from overriding each others, but I'd prefer that once authenticated via the UI, the API can use the same cookie/bearer token. I can't find anything online but I am new to ASP.Net development to know if I'm searching for the right thing.
What's the best/correct approach for this?
Thanks!
Tom
The recommended approach is:
The web UI redirects authentication of the end user to Azure AD B2C. If authenticated, then it acquires an access token from Azure AD B2C and sends this access token in the Authorization header to the web API.
The web API receives the access token in the Authorization header from the web app and validates it. If valid, then it allows the API call; otherwise, it denies it.
An example of this can be found in the Azure AD B2C: Call an ASP.NET Web API from an ASP.NET Web App sample.

Token based authentication with different types of clients in C#

I am working on a project where we have different type of clients (winform App, ASP.net mvc, WCF service).
I need to implement security in it. Here is my plan...
There will be one service (planning to create as WebApi) to authenticate user.
client app will pass Windows identity to this authentication service.
Service will return authentication Token to user.
This token will be passed to WCF service for each call.
WCF service will verify this token using authentication service.
If user has correct permissions, then WCF will perform requested action.
If token expire then WCF service will renew the token using Auth service.
I am planning to use attribute based programming for Security for each WCF call.
I gone through lots of articles online but could worked out it.
can anyone point me to some article where I an find below...
Generate token based on Windows Identity.
How to pass token to WCF service for each call.
Thanks in advance.
I suggest using Access Token for authentication and refreshtoken to obtain renewed access token. You can store refreshtokens in database. If your scenario needs multiple client/device for each user then you will need to store unique deviceId with refreshtoken.Below helpful links for Access Token and RefreshToken:
Token Based Authentication using ASP.NET Web API 2, Owin, and Identity
Enable OAuth Refresh Tokens in AngularJS App using ASP .NET Web API 2, and Owin

Token based authentication in web api

I am developing a web api service which will be consumed by asp.net mvc web application and mobile apps (android and ios). My database and web api will be hosted on same server. I want to implement authentication for web api (token based). My database has a table for storing user credentials. I will have to use this database to validate the credentials when the user logs in.
Came across a few articles which suggest owin and identity server. I am confused on how to proceed and need some help in understanding a better approach.
you can simply implement the token base authentication
when user is login to the system send the random token code
cash it in the client side
when client side making request to the server send the token with the request header
validate the token and accept request or reject
simple way :)

Authorization with OAuth Bearer Token provided by External Web API

I'm posting this in the hope of receiving some feedback/advice and information on something I've been struggling with the last few days. To start I'll give a quick breakdown of the project.
There are 2 applications in the solution:
WebAPI resource & authorization server - Uses OWIN (hosted in IIS) and ASP.NET Identity to issue an authentication token on a correct login and then allow requests to the various controllers.
MVC client application - Has no authorization as of yet (until I figure it out) but will make calls to the WebAPI resource server to get all data. These calls will only ever be made from actions of the controllers in the client app, no client side AJAX calls.
The client application doesn't have it's own datasource. All information is stored in a database which the WebAPI service has access to, so essentially if they provide the correct credentials and the client app receives a bearer token I need to deliver a way for the application to see them as authorized.
What's the best way to handle this?
Is it possible to configure OWIN on the client side to use the OAuth
settings of the server? Am I barking up the wrong tree and will I need to just use HTTPClients?
Could I deserialize the bearer token and store it in session, and
then write my own authorization providers to check these on the client side?
My initial concerns are I'm abusing Bearer Tokens and trying to cobble them into a solution which isn't ideal. All examples of external authorization I've found so far usually involve making calls to providers hosted by Google/Facebook/Twitter to check the user is who they say they are, and then moves on to creating a user record in their system. My application can't do this.
In regards to security I was planning to introduce filters which would validate the request has came from the client application by providing an identifier and secret, along with IP validation.
I realize this may be a bit open ended, but I'd appreciate any advice. The scope of the project is that the web service is the only thing to have access to the database. The MVC client application will be hosted on a different server, and the service will only accept requests from said client application.
You don't need to access the data source in your MVC app to validate the bearer token. Basically, you can do it in the following way,
MVC app requests access_token from webapi and passes it to the UI client (let's say a browser).
Browser stores the access_token in a cookie/localstorage and sends them to the MVC app for all subsequent requests.
Create an ActionFilter in the MVC app to validate if the request from the browser has the token supplied in the header. If not, reject the request.
MVC app passes the access_token in the Authorization header to the webapi.
Use HTTPS for all communications (between MVC app <-> Client and MVC app <-> WebAPI)
You can further obfuscate or encrypt the access_token you get from the WebAPI on the MVC app side for additional security but then you will have to send the decrypted version back to the WebAPI.
I realize that my answer is a bit late, but maybe it helps other people:
The bearer token that you get from the API has a list of claims encrypted that only the API can decrypt. I assume you need to have these claims on the MVC application as well so you can restrict resources on the client.
So, what I have done was to first get token. After you get it, you make another request to the API resource api/me/claims to get the list of readable claims on the client. Based on this list you can allow access to resources in your MVC CLient Application using a custom claims based Authorize attribute. Also, you can store the claims in a cookie in the client session. Below is the code for the API controller to get the Claims.
[RoutePrefix("api/me/claims")]
public class ClaimsController : BaseApiController
{
[Authorize]
[Route("")]
public IHttpActionResult GetClaims()
{
var identity = User.Identity as ClaimsIdentity;
var claims = from c in identity.Claims
select new
{
subject = c.Subject.Name,
type = c.Type,
value = c.Value
};
return Ok(claims);
}
}
The idea is to reconstruct the ClaimsIdentity object of the logged User on the Client side and maybe add it to the session.
The token is not enough. You might risk getting a Not Authorized response from the API on a resource that you have made visible for the user in the MVC Client Application. Needles to say that is recommended to use HTTPS for all requests.

Authenticate MVC client against self-hosted Web API (OWIN/Katana)

I have a Windows Service running a Web API hosted as a OWIN middleware - the server. The API uses application cookie authentication and validates users against a database, using OWINs identity model. Now I would like to authenticate a user who accesses the API through a standard MVC web application (the client), but I'm unsure how to achieve this, e.g. after I received a response along with the cookie from the API, where do I have to store it inside the MVC application so that the cookie will be automatically sent along with further API calls.
You won't need to. Cookies are stored by the client's browser, and are sent to the web server with every request on the same domain name. Each subdomain will have its own sandbox for cookies. The main domain's cookies can be accessed by all subdomains.
MVC application will store it in the users browser the cookie. If you need to find an alternate way to achieve it, why not try localstorage. You can then send the authorization token with every request header using your Ajax calls. If you are interested in an Angular Application, here is an excellent tutorial that should help clarify a lot of question.

Categories