【问题标题】:Azure AD authentication with asp.net Identity for authorisation使用 asp.net 身份进行授权的 Azure AD 身份验证
【发布时间】:2016-08-24 09:57:11
【问题描述】:

我试图在整个互联网上寻找,但看不到如何实现我被要求的目标。这是我的企业应用程序,它使用 Asp.net Identity 进行基于表单的身份验证。我已经扩展了用户和角色以及组以在我的代码中提供授权。 (注意:不使用任何组/角色指令)。

现在我被要求研究更改代码以适应 Azure Active Directory 身份验证的可能性。我尝试阅读如何注册应用程序、将用户发送到 Azure 站点进行身份验证、取回令牌等。但是我被困在“之后怎么办?”我已经通过身份验证的用户如何使用我现有的 Asp.net Identity 模型,其中用户存储在 sql 数据库中。如何使用此令牌关联现有用户。

此外,当我将项目更改为允许 Azure AD 时,它会删除 Aspnet.Identity 包,因为它与 Azure AD 不兼容!!

我什至尝试手动将两个包并排放置,我必须指出用户被发送到 Azure 上进行身份验证的位置,然后转到主页并再次以永无止境的循环登录 Azure AD。

总结问题,我如何从 AAD 验证用户并继续使用现有的角色和组授权。

编辑: 我尝试创建单独的 Web 服务来验证用户并发送 JWT 令牌。如果我直接在浏览器上调用它可以找到,但是,当我尝试从我的网络应用程序调用此服务时,我得到了奇怪的错误

 Application with identifier 'a2d2---------------' was not found in the directory azurewebsites.net

这里的奇怪部分是目录名称是“azurewebsites.net”,而不是我使用的默认目录。

更新 这是引发错误的代码

public async Task<ActionResult> Login(string returnUrl)
        {


            try
            {

                // get the access token
                AuthenticationContext authContext = new AuthenticationContext(authority, new TokenCache());

                var clientCredential = new ClientCredential(clientId, password);
//Error on below line
                AuthenticationResult result = await authContext.AcquireTokenAsync(resourceId, clientCredential);


                // give it to the server to get a JWT
                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
......

【问题讨论】:

    标签: c# asp.net-mvc authentication azure-active-directory


    【解决方案1】:

    试试这个:

    var client = new RestClient("https://login.microsoftonline.com/{tenant- 
             Id}/oauth2/v2.0/token");
    var request = new RestRequest(Method.POST);         
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("content-type", "application/x-www-form-urlencoded");         
    request.AddHeader("grant_type", "password");
    request.AddParameter("application/x-www-form-urlencoded", 
            "grant_type=password&client_id={client-Id}&client_secret={client- 
            secret}&scope={scopeurl}&userName={username}&password={password}", 
            ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    var json = response.Content;
    var JSONObject = JObject.Parse(json);
    var token = (string)JSONObject["access_token"];
    

    【讨论】:

    • 这需要更多细节。 RestClient 是什么?在哪里可以找到它?为什么使用它而不是 HttpClient ?这个答案看起来也没有问题本身的代码那么干净。
    【解决方案2】:

    我遇到了类似的问题,所以我创建了一个 Office 365 owin 安全插件。我在 github 上分享了代码。它基于 codeplex 的 katana project

    您可以在https://github.com/chadwjames/wakizashi找到源代码。

    您需要注册您的应用程序here。注册应用程序时,将回调 uri 设置为https://yourdomain/signin-office365

    应用程序 ID 是您的客户端 ID,密码是您的客户端密码。

    注册后,您可以修改 Startup.Auth.cs 并将类似的内容添加到 ConfigureAuth 方法中。

            //setup office 365
            var office365Options = new Office365AuthenticationOptions
            {
                ClientId = ConfigurationManager.AppSettings["ada:ClientId"],
                ClientSecret = ConfigurationManager.AppSettings["ada:ClientSecret"],
                Provider = new Office365AuthenticationProvider
                {
                    OnAuthenticated = async context =>
                    {
                        await
                            Task.Run(
                                () => context.Identity.AddClaim(new Claim("Office365AccessToken", context.AccessToken)));
                    }
                },
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
            };
    
            office365Options.Scope.Add("offline_access");
            app.UseOffice365Authentication(office365Options);
    

    当我有更多时间时,我希望为此创建一个 nuget 包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 2019-04-30
      • 2021-09-16
      • 2019-11-16
      相关资源
      最近更新 更多