第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從 .Net Core 中的單獨(dú)類庫(kù)中獲取經(jīng)過(guò)身份驗(yàn)證的用戶

如何從 .Net Core 中的單獨(dú)類庫(kù)中獲取經(jīng)過(guò)身份驗(yàn)證的用戶

C#
慕碼人8056858 2021-06-28 13:51:00
我有一個(gè)引用 .NET Core 類庫(kù)的 MVC 核心應(yīng)用程序。所有的數(shù)據(jù)訪問(wèn)和業(yè)務(wù)邏輯都在類庫(kù)中。如何從類庫(kù)訪問(wèn)經(jīng)過(guò)身份驗(yàn)證的用戶?過(guò)去使用 .NET Framework 您可以使用string UserName = System.Web.HttpContext.Current.User.Identity.Name從類庫(kù)中的方法內(nèi)部獲取用戶名。在 .NET Core 中,似乎HttpContext不再具有CurrentorUser屬性。這是一個(gè)簡(jiǎn)單的用例。假設(shè)我有一個(gè)數(shù)據(jù)實(shí)體和服務(wù),它在將實(shí)體保存到數(shù)據(jù)庫(kù)之前用日期和用戶名“標(biāo)記”實(shí)體。這些將在外部類庫(kù)中:public interface IAuditable{    DateTime CreateDate{get;set;}    string UserName{get;set;}}public class MyEntity:IAuditable{    public int ID{get;set;}    public string Name{get;set;}    public string Information{get;set;}}public static class Auditor{    public static IAuditable Stamp(IAuditable model){                    model.CreateDate=DateTime.UtcNow;        model.CreatedBy=System.Web.HttpContext.Current.User.Identity.Name;        return model;    }}public sealed class MyService:IDisposable{    MyDb db=new MyDb();    public async Task<int> Create(MyEntity model){        Auditor.Stamp(model);        db.MyEntities.Add(model);        return await db.SaveAsync();    }    public void Dispose(){        db.Dispose();    }}然后在我的 MVC 控制器中,我有一個(gè)調(diào)用服務(wù)的 post 操作:[HttpPost][ValidateAntiForgeryToken]public async Task<IActionResult> Create(MyEntity model){    await service.Create(model);    return RedirectToAction("Index")}我想要一種方法來(lái)替換該行,Auditor.Stamp因?yàn)镠ttpContext.Current.NET Core 顯然沒(méi)有。
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

在這個(gè)版本中仍然存在同樣的靈活性。


注入IHttpContextAccessor并且您可以訪問(wèn)您需要的內(nèi)容。


重構(gòu)靜態(tài)Auditor以使其更具可注入性


public interface IAuditor {

    IAuditable Stamp(IAuditable model);

}


public class Auditor : IAuditor {

    private readonly IHttpContextAccessor accessor;


    public Auditor(IHttpContextAccessor accessor) {

        this.accessor = accessor;

    }


    public IAuditable Stamp(IAuditable model){            

        model.CreateDate = DateTime.UtcNow;

        model.CreatedBy = accessor.HttpContext.User?.Identity?.Name;

        return model;

    }

}

然后服務(wù)將依賴于新的抽象


public interface IMyService : IDisposable {

    Task<int> Create(MyEntity model);

}


public sealed class MyService : IMyService {

    MyDb db = new MyDb();

    private readonly IAuditor auditor;


    public MyService(IAuditor auditor) {

        this.auditor = auditor;

    }


    public async Task<int> Create(MyEntity model) {

        auditor.Stamp(model);

        db.MyEntities.Add(model);

        return await db.SaveAsync();

    }


    public void Dispose() {

        db.Dispose();

    }

}

您確實(shí)也應(yīng)該注入 ,MyDb但這超出了當(dāng)前問(wèn)題的范圍。


最后,您將庫(kù)配置為能夠在啟動(dòng)期間設(shè)置服務(wù)


public static IServiceCollection AddMyLibrary(this IServiceCollection services) {


    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    services.AddSingleton<IAuditor, Auditor>();  

    services.AddScoped<IMyService, MyService>();


    //...add other services as needed


    return services.  

}

然后你可以在從根項(xiàng)目啟動(dòng)時(shí)調(diào)用它


public void ConfigureServices(IServiceCollection services) {


    //...


    services.AddMyLibrary();


    //...

}

通過(guò)這些抽象和使用 DI 應(yīng)用程序部分是解耦的,可以單獨(dú)測(cè)試。


查看完整回答
反對(duì) 回復(fù) 2021-07-03
  • 1 回答
  • 0 關(guān)注
  • 270 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)