我试图在整个互联网上寻找,但看不出如何才能实现我被要求的目标。这是我的企业应用程序,它使用 Asp.net Identity 进行基于表单的身份验证。我扩展了用户和角色以及组以在我的代码中提供授权。 (注意:不使用任何组/角色指令)。
现在我被要求研究更改代码以适应 Azure Active Directory 身份验证的可能性。我尝试阅读有关如何注册应用程序、将用户发送到 Azure 站点进行身份验证、取回 token 等的信息。但是我一直停留在“之后呢?”我有经过身份验证的用户如何使用我现有的 Asp.net Identity 模型,其中用户存储在 sql 数据库中。如何使用此 token 关联现有用户。
此外,当我更改我的项目以允许使用 Azure AD 时,它会删除 Aspnet.Identity 包,因为它与 Azure AD 不兼容!!
我什至尝试手动将两个包并排放置,我必须指出用户被发送到 Azure 进行身份验证的位置,转移回主页并再次登录 Azure AD,永无止境。
总结一下这个问题,我如何从 AAD 中验证用户身份并继续使用现有的角色和组授权。
编辑: 我尝试创建单独的 Web 服务,它将对用户进行身份验证并发送 JWT token 。如果我直接在浏览器上调用它,它会起作用,但是,当我尝试从我的网络应用程序调用此服务时,我会收到奇怪的错误
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"];
关于c# - 使用 asp.net Identity 进行 Azure AD 身份验证以进行授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39120051/