c# - 使用 Azure Active Directory 和使用 Entity Framework 的 Azure SQL 验证 ASP.NET Core 2.1 Web API
问题描述
我已经在 Azure SQL 数据库上实现了 Azure AD 身份验证,并想为其编写一个使用相同 AD 身份验证的 Web API,但我需要将该身份验证从 Web API 传递到 Azure SQL 连接。我们还希望在这个项目中使用实体框架。
Web API 的 Startup 类中的UseSqlServer
函数通过带有身份验证详细信息的连接字符串,所以我想解决这个问题的方法是根据使用 Web API 进行身份验证的用户以某种方式使连接动态化。
这是我们正在查看的更图形化的地图:
| azure active directory |
|| ||
\/ \/
client ------> | web api | ------> | azure sql |
解决此问题的最佳方法是什么?
解决方案
您是否考虑过使用令牌进行身份验证?您将在此处找到示例应用程序。
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TodoListService.DAL
{
public class PerWebUserCache
{
[Key]
public int EntryId { get; set; }
public string webUserUniqueId { get; set; }
public byte[] cacheBits { get; set; }
public DateTime LastWrite { get; set; }
}
public class DbTokenCache : TokenCache
{
private TodoListServiceContext db = new TodoListServiceContext();
string User;
PerWebUserCache Cache;
// constructor
public DbTokenCache(string user)
{
// associate the cache to the current user of the web app
User = user;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
this.BeforeWrite = BeforeWriteNotification;
// look up the entry in the DB
Cache = db.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
// place the entry in memory
this.Deserialize((Cache == null) ? null : Cache.cacheBits);
}
// clean up the DB
public override void Clear()
{
base.Clear();
foreach (var cacheEntry in db.PerUserCacheList.Where(u => u.webUserUniqueId == User).ToList())
db.PerUserCacheList.Remove(cacheEntry);
db.SaveChanges();
}
// Notification raised before ADAL accesses the cache.
// This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
if (Cache == null)
{
// first time access
Cache = db.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
}
else
{ // retrieve last write from the DB
var status = from e in db.PerUserCacheList
where (e.webUserUniqueId == User)
select new
{
LastWrite = e.LastWrite
};
// if the in-memory copy is older than the persistent copy
if (status.First().LastWrite > Cache.LastWrite)
//// read from from storage, update in-memory copy
{
Cache = db.PerUserCacheList.FirstOrDefault(c => c.webUserUniqueId == User);
}
}
this.Deserialize((Cache == null) ? null : Cache.cacheBits);
}
// Notification raised after ADAL accessed the cache.
// If the HasStateChanged flag is set, ADAL changed the content of the cache
void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if state changed
if (this.HasStateChanged)
{
Cache = new PerWebUserCache
{
webUserUniqueId = User,
cacheBits = this.Serialize(),
LastWrite = DateTime.Now
};
//// update the DB and the lastwrite
db.Entry(Cache).State = Cache.EntryId == 0 ? EntityState.Added : EntityState.Modified;
db.SaveChanges();
this.HasStateChanged = false;
}
}
void BeforeWriteNotification(TokenCacheNotificationArgs args)
{
// if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
}
}
}
推荐阅读
- excel - 使用 range.validation 删除/锁定下拉列表
- flutter - Flutter 使用 AccountManager 添加帐户?
- solr - 您如何与父对象一起返回 Solr 子对象和孙对象?
- python - 在不规则维度的列表中进行比较和算术的有效方法,Python
- azure - 具有自动备份功能的 Azure 分析数据库
- json - 如何在 Visual Studio Code 中控制 JSON 的格式样式?
- aem - AEM Querybuilder 获取(如果引用)
- if-statement - Else 语句没有被执行 - Unix
- angular - Angular 8在完成多个后续请求时订阅,如何?
- java - 为什么在循环中使用“str.charAt(i) = str.charAt(i+1)”时会出错