繁体   English   中英

使用 asp.net Identity 进行 Azure AD 身份验证以进行授权

[英]Azure AD authentication with asp.net Identity for authorisation

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

现在我被要求研究更改代码以适应 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);
......

试试这个:

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"];

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

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

您需要在此处注册您的应用程序。 注册应用程序时将回调 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 包。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM