用asp.net进行Azure AD身份验证身份验证

用asp.net进行Azure AD身份验证身份验证

问题描述:

我试图通过网络查找所有内容,但无法看到我如何实现所要求的内容。这是我的企业应用程序,它使用Asp.net身份进行基于表单的身份验证。我已将用户和角色与组一起扩展以在我的代码中提供授权。 (注意:不使用任何组/角色指令)。用asp.net进行Azure AD身份验证身份验证

现在我被要求考虑更改代码以适应Azure Active Directory身份验证的可能性。我尝试阅读如何注册应用程序,将用户发送到Azure站点进行身份验证,取回令牌等。但是,我被困在'事后?'中。我已验证用户如何使用我的现有Asp.net身份模型,其中用户存储在SQL数据库中。如何使用此令牌关联现有用户。

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

我甚至尝试手动同时保存两个包,我得到指向用户发送到Azure上进行身份验证的位置,转移回主页并再次登录Azure AD的永无止境的循环。

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

编辑︰ 我试着创建单独的Web服务,将验证用户和发送JWT令牌。它的工作原理,发现如果我直接调用它的浏览器,但是当我试图从我的web应用程序调用这个服务,我得到奇怪的错误

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); 
...... 

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

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

您需要注册您的应用程序here。在注册应用程序时,将回拨设置为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包。