gpt4 book ai didi

c# - 在 ASP.NET Web 应用程序之外获取 UserManager 实例?

转载 作者:行者123 更新时间:2023-11-30 17:29:06 25 4
gpt4 key购买 nike

我有一个 ASP.NET MVC 5 网络应用程序和 ASP.NET Identity(个人账户)。但我需要能够从控制台应用程序注册新用户。

因此,我将一些 ASP.NET Identity 类从 Web 应用程序移动到类库中,以便在 Web 应用程序和 CLI 之间共享。

我已经成功移动了以下内容:

public class PortalDbContext : IdentityDbContext<PortalUser>
{
public PortalDbContext(string connectionString)
: base(connectionString, throwIfV1Schema: false)
{
}

public static PortalDbContext Create(string connectionString)
{
return new PortalDbContext(connectionString);
}
}

public class PortalUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<PortalUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

// Add custom user claims here
return userIdentity;
}
}

public class PortalUserManager : UserManager<PortalUser>
{
public PortalUserManager(IUserStore<PortalUser> store) : base(store)
{
}

public async Task<IdentityResult> RegisterUser(string email, string password)
{
PortalUser user = new PortalUser { UserName = email, Email = email };

return await this.CreateAsync(user, password);
}
}

但我不知道从哪里得到 IUserStore<PortalUser> PortalUserManager需要从。

在网络应用程序中,此管理器是从 HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>() 中检索到的我显然不能在类库中使用它。

最佳答案

看看 OwinRequestScopeContext核包。它允许您在不依赖 System.Web 的情况下使用上下文。为了没有仅链接的答案,我将添加当前自述文件中的示例:

# Usage 

// using Owin; you can use UseRequestScopeContext extension method.

// enabled timing is according to Pipeline.
// so I recommend enable as far in advance as possible.
app.UseRequestScopeContext();

app.UseErrorPage();
app.Run(async _ =>
{
// get global context like HttpContext.Current.
var context = OwinRequestScopeContext.Current;

// Environment is raw Owin Environment as IDictionary<string, object>.
var __ = context.Environment;

// optional:If you want to change Microsoft.Owin.OwinContext, you can wrap.
new Microsoft.Owin.OwinContext(context.Environment);

// Timestamp is request started(correctly called RequestScopeContextMiddleware timing).
var ___ = context.Timestamp;

// Items is IDictionary<string, object> like HttpContext.Items.
// Items is threadsafe(as ConcurrentDictionary) by default.
var ____ = context.Items;

// DisposeOnPipelineCompleted can register dispose when request completed(correctly RequestScopeContextMiddleware underling Middlewares finished)
// return value is cancelToken. If call token.Dispose() then canceled register.
var cancelToken = context.DisposeOnPipelineCompleted(new TraceDisposable());

// OwinRequestScopeContext over async/await also ConfigureAwait(false)
context.Items["test"] = "foo";
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
var _____ = OwinRequestScopeContext.Current.Items["test"]; // foo

await Task.Run(() =>
{
// OwinRequestScopeContext over new thread/threadpool.
var ______ = OwinRequestScopeContext.Current.Items["test"]; // foo
});

_.Response.ContentType = "text/plain";
await _.Response.WriteAsync("Hello OwinRequestScopeContext! => ");
await _.Response.WriteAsync(OwinRequestScopeContext.Current.Items["test"] as string); // render foo
});

关于c# - 在 ASP.NET Web 应用程序之外获取 UserManager 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51793313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com