Passing credentials from a web application to a web API. Azure Authentication Authentication

I have a regular ASP.NET web application that I deployed to Azure using Application Services . After I deployed it, I turned on application service authentication and configured Azure Active Directory. This allowed me to publish my web application, and also get authentication so that only those who are members of Active Directory can log in.

enter image description here

, - ASP.NET . , - , -API ( SQL ), . , API, Azure Active Directory - -API.

, -, -API.

public string GetStringAsync(string endPoint)
{
    HttpClientHandler handler = new HttpClientHandler
    {
        UseDefaultCredentials = true
    };
    handler.PreAuthenticate = true;
    using (HttpClient client = new HttpClient(handler))
    {
        return client.GetStringAsync(endPoint).Result;
    }
}

, API, . , Identity , API:

HttpContext.Current.User.Identity.Name;

\ Azure Active Directory , - -API? , API ?

+4
3

- Azure - Azure AD.

AFAIK, -API. , -API Azure AD ( , -), access_token -API. -APP -API access_token.

auth -API, ( 2: App App REST API) APP ID , -API. access_token -, Graph API .

+1

1- cookie.ASPX auth -

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
string authToken = authCookie.Value;

2- cookie httpclient. cookie .ASPX auth cookie.

cookie HttpRequestMessage HttpClient

3- -api HttpContext.Current.User.Identity.Name.

, .

0
[Authorize]
public IEnumerable<Object> Get()
{
 var owner = ObtainCurrentOwner();
 var assets = GetAssets(owner.Id);
 return result;
}

protected Owner ObtainCurrentOwner()
{
 return RavenSession.Query<Owner>().SingleOrDefault(x => 
           x.UserName == HttpContext.Current.User.Identity.Name);
}

public IEnumerable<Asset> GetAssets(int ownerID)
{
 return RavenSession.Query<Asset>().Where(x => x.OwnerId == ownerID);
}

This method is decorated with the [Authorize] attribute. This mechanism was previously known in WCF. ASP.NET validates the cookie as part of this request, and if the cookie is not present, the request is rejected. Retrieving the current user and all of its assets is associated with two LINQ queries using the RavenSession, which must be opened earlier.

https://www.codeproject.com/Articles/568115/Sample-application-RavenDB-KnockoutJS-Bootstrap-We

0
source

Source: https://habr.com/ru/post/1681161/


All Articles