ASP. net Rest API перенаправляется на login.microsoftonline.com - PullRequest
0 голосов
/ 17 июня 2020

Я конвертирую приложение Webforms из использования локальной LDAP Auth (System.DirectoryServices.AccountManagement, System.Security) в Azure AD с ролями SSO и AAD.

SSO работает, но когда я пытаюсь получить доступ к одному из моих Rest API, я получаю HTTP-код ответа 302 (Найдено / перенаправлено) на / от https://login.microsoftonline.com - очевидно, из-за SSO.

API написаны в коде за файлом (aspx.cs) таким образом

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<someclass> GetThings()
    {
        List<someclass> dict = new BusinessObjects().GetStuff();
        return dict;
    }

Что здесь не так? Нужно ли мне добавлять аутентификацию в API? должен ли это быть токен / носитель?

мой StartupAuth.cs выглядит так:

        public void ConfigureAuth(IAppBuilder app)
    {            
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,

                Notifications = new OpenIdConnectAuthenticationNotifications()
                {
                    AuthenticationFailed = (context) =>
                    {
                        return System.Threading.Tasks.Task.FromResult(0);
                    },
                    //custom code
                    SecurityTokenValidated = (context) =>
                    {
                        var claims = context.AuthenticationTicket.Identity.Claims;
                        //add Azure Active Directory Groups to the context. Requires "groupMembershipClaims": "SecurityGroup",  in manifest file in Azure Portal
                        var groups = from c in claims
                                     where c.Type == "groups"
                                     select c;

                        foreach (var group in groups)
                        {
                            context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, group.Value));
                        }

                        //add Azure Active Directory Roles to the context. Requires entries in "appRoles": [] in manifest file in Azure Portal
                        var roles = from r in claims
                                    where r.Type == "roles"
                                    select r;

                        foreach (var role in roles)
                        {
                            context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, role.Value));
                        }

                        return Task.FromResult(0);
                    },
                    //AuthorizationCodeReceived = (context) => {

                    //}

                }

            }
            );

        // This makes any middleware defined above this line run before the Authorization rule is applied in web.config
        app.UseStageMarker(PipelineStage.Authenticate);  
    }
}
...