将 nHibernate 与访问令牌和托管身份结合使用

时间:2021-03-03 09:03:53

标签: entity-framework asp.net-core nhibernate azure-sql-database

有一个有趣的问题,目前正在构建一个需要使用托管标识连接到 azure sql 的 Web 应用程序,我正在尝试从 nhibernate 创建到 sql 的连接,但使用访问令牌。现在,我已经为实体框架完成了这项工作,它运行良好,但由于某种原因,我无法让它在 nhibernate 上运行。

我的EF代码如下:

def search(request):
    if request.method == 'GET':      
        product_name =  request.GET.get('search')    
        try:
            product = Product.objects.filter(name__icontains=product_name)
            context = {"product": product}
        except:
            product = "What is this"
            context = {"product":product}

        return render(request, "core/search.html", context)

    else:
        result = "Sorry there is no product with that name"
        return render(request, "core/search.html", {"result":result})

这工作得很好,但是当尝试用 nhibernate 做类似的事情时,事情就不那么好了。

public MyDbContext(IDBAuthTokenService tokenService)
{
    var connection = this.Database.GetDbConnection() as SqlConnection;
    if (!connection.ConnectionString.Contains("Trusted_Connection"))
    {
        connection.AccessToken = tokenService.GetToken();
    }
}

我是否走在正确的道路上?可以这样做吗?

任何帮助将不胜感激。

0 个答案:

没有答案