简体   繁体   English

如何在我的 web 表单应用程序中调用 Azure AD 服务

[英]how to call Azure AD service in my web form application

I have a WebForms project and some Web Service.My web services are projected by Azure AD.我有一个 WebForms 项目和一些 Web 服务。我的 web 服务由 Azure AD 投影。 So I need to acquire AD access token in my web from project.所以我需要从项目中获取我的 web 中的 AD 访问令牌。 But I cannot find any sample about how to integrate Azure AD with web form project in Microsoft official sample.但是我在微软官方示例中找不到任何关于如何将 Azure AD 与 web 表单项目集成的示例。 Could someone help me on this?有人可以帮我吗?

If you want to call the service protected by Azure AD, you can use OpenId Connect protocol.如果要调用受Azure AD保护的服务,可以使用OpenId Connect协议。

For example:例如:

Add a Startup.cs file in your project在项目中添加 Startup.cs 文件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Web;
using Owin;
using Microsoft.Owin.Extensions;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Microsoft.Owin.Security.Notifications;
using Microsoft.IdentityModel.Protocols;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;

namespace TestWebForm
{
    public partial class Startup
    {
        private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
        private static string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["ida:AADInstance"]);
        private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
        private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
        private static string redirectUri = postLogoutRedirectUri;
        private static string clientSecret = ConfigurationManager.AppSettings["ida:ClientSecret"];
        string authority = aadInstance + tenantId;

        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);
                        },
                        AuthorizationCodeReceived = OnAuthorizationCodeReceived
                    }

                }
                );


        }
        private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
        {
            context.HandleResponse();
            context.Response.Redirect("/?errormessage=" + context.Exception.Message);
            return Task.FromResult(0);
        }


        private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
        {
            var idClient = ConfidentialClientApplicationBuilder.Create(clientId)
                .WithRedirectUri(redirectUri)
                .WithClientSecret(clientSecret)
                .Build();

            string[] scopes = { "user.read" };

            var result = await idClient.AcquireTokenByAuthorizationCode(
                scopes, notification.Code).ExecuteAsync();

            GenUtil.token = result.AccessToken;
        }

        private static string EnsureTrailingSlash(string value)
        {
            if (value == null)
            {
                value = string.Empty;
            }

            if (!value.EndsWith("/", StringComparison.Ordinal))
            {
                return value + "/";
            }

            return value;
        }


    }
}

For more details, please refer to Azure AD Authentication in Asp.net web forms web application . For more details, please refer to Azure AD Authentication in Asp.net web forms web application .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Web服务中验证Azure AD Web表单令牌 - How to validate Azure AD web form token in web service 如何为我的 React/.NET Core 3.0 SPA web 应用程序添加 Microsoft Identity/Azure AD 登录功能 - How to add Microsoft Identity/Azure AD login feature for my React/.NET Core 3.0 SPA web application 如何使用Web应用程序/ WebAPI验证Azure AD中的用户凭据 - How to validate user credentials in Azure AD with Web application / WebAPI 如何在我的Android应用程序的Visual C#Web服务中调用LINQ中的用户定义函数? - How to call user defined function in LINQ in my visual C# web service for my android application? 我的桌面应用程序挂起了使用async和await的Web API服务调用 - My desktop application hangs on web API service call with async and await 如何保护Web API和应用程序使用的Azure SQL数据库(Azure AD) - How to secure Azure SQL database used by Web API and Application (Azure AD) 如何从Web应用程序调用Web服务的更新功能 - How to call update function of web service from web application Asp.net Web窗体Web应用程序中的Azure AD身份验证 - Azure AD Authentication in Asp.net web forms web application Web服务Windows窗体应用程序 - Web service windows form application 如何将Web服务添加到Web应用程序? - How can i add web service to my web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM