首页 文章

使用c#web api进行Microsoft Azure身份验证

提问于
浏览
2

我是Azure,C#和Web API的新手,所以首先我想提前道歉,因为这听起来像是一个愚蠢的问题 .

我只是想知道,当我在本地开发我的C#Web API和我的前端Web应用程序时,我是否已经使用Azure的身份验证了?喜欢使用某人的Microsoft帐户进行身份验证?

谢谢!

EDIT

所以我已经根据这个链接设置了应用程序:https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-how-to-configure-microsoft-authentication

在我的前端Web应用程序上,当用户未经过身份验证时,它会被重定向到Microsoft的登录页面,当我尝试登录时,我收到以下错误:

enter image description here

Also please note I haven't deployed anything to Azure yet, my codes are still local I just need the authentication for now.

希望有人能再次帮助我 .

谢谢

UPDATE

所以我能够通过使用某人创建的msalx库来实现MSAL,这使得可以在Angular中集成msal . 在使用我的个人Microsoft帐户登录后,我已经可以获得访问令牌,但问题是当我访问我的Web API时,它总是说401 Unauthorized .

提前致谢!

enter image description here

2 回答

  • 2

    AFAIK,对于使用App Service Authentication / Authorization,您的C#Web API需要部署到azure . 应用服务身份验证/授权( Easy Auth )是Azure应用服务的一项功能,可以作为本机IIS模块实现,该模块与您的Azure应用程序在同一个沙箱中运行 . 有关更多详细信息,请参阅Architecture of Azure App Service Authentication / Authorization .

    根据您的方案,您可以参考以下方法:

    Use App Service Authentication / Authorization (Easy Auth)

    enter image description here

    有关如何对用户进行身份验证的更多详细信息,请参阅How to: Authenticate users .

    Note: 对于本地SPA,您需要配置CORS设置并在您的azure Web应用程序的"SETTINGS > Authentication / Authorization"下添加 ALLOWED EXTERNAL REDIRECT URLS . 有关更多详细信息,请参阅此issue .

    Set up the authentication in your Web API

    • 您可以设置OAuth 2.0承载认证,使用AD v2.0 endpoints ,您可以使用MSA和工作或学校帐户来保护您的Web API . 有关如何构建Web API,可以参考Secure an MVC web API .

    • 对于您的前端Web应用程序,您可以利用MSAL.js库来记录和检索access_token,并使用该令牌来调用Web API HTTP bearer request .

    UPDATE:

    我使用AppModelv2-WebAPI-DotNetTodoListService 项目,然后使用以下html为我的客户端,如下所示:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Login</title>
        <meta charset="utf-8" />
    
        <!-- IE support: add promises polyfill before msal.js  -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.4/bluebird.min.js" class="pre"></script>
        <script src="https://secure.aadcdn.microsoftonline-p.com/lib/0.1.1/js/msal.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
        <script class="pre">
        var userAgentApplication = new Msal.UserAgentApplication("b3dd78af-d9da-459d-bf01-f87104d87450", null, function (errorDes, token, error, tokenType) {
            // this callback is called after loginRedirect OR acquireTokenRedirect (not used for loginPopup/aquireTokenPopup)
        });
    
        userAgentApplication.loginPopup(["user.read"]).then(function (token) {
            var user = userAgentApplication.getUser();
            console.log(user);
            console.log('access token:' + token);
    
            InvokeApi(token);
    
        }, function (error) {
            alert('login error:' + error);
        });
    
        function InvokeApi(token) {
            $.ajax({
                type: "GET",
                url: "http://localhost:9184/api/values",
                headers: {
                    'Authorization': 'Bearer ' + token,
                },
            }).done(function (data) {
                console.log('invoked Web API \'http://localhost:9184/api/values\' with result: ' + data);
                alert(data);
            }).fail(function (jqXHR, textStatus) {
                alert('Error getting data');
            });
        }
        </script>
    </body>
    </html>
    

    Result:

    enter image description here

    另外,你可以参考这个git示例JavaScript Single Page Application with an ASP.NET backend, using msal.js .

  • 1

    根据我的理解,您正在尝试使用其Microsoft帐户对用户进行身份验证 .

    以下是有关如何设置和启用Web应用程序以使用基于Microsoft帐户的身份验证的有用指南 .

    https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-how-to-configure-microsoft-authentication

    此外,在设置和配置本地应用程序后,您可以在本地测试它,这是肯定的 .

    这是一个使用Microsoft帐户身份验证的GitHub repo示例Web应用程序 .

    https://github.com/lexdevel/MicrosoftAccountAuthentication

相关问题