gpt4 book ai didi

asp.net - 我应该如何处理 Identity 2.0、WebAPI 2.1 和 Owin 2 的身份验证?

转载 作者:行者123 更新时间:2023-12-02 21:24:27 34 4
gpt4 key购买 nike

我正在使用

  • 同一域中的新浏览器客户端
  • 身份2.0
  • WebAPI 2.1
  • 欧文2.1
  • AngularJS 前端,用于注册、登录和数据显示

在具有 AngularJS 前端的 WebAPI 应用程序中。

我正在阅读有关 token 身份验证的内容,但我现在很困惑,我找不到任何使用我的组合的好例子。我想知道的是我应该使用 cookie 还是 token 进行身份验证。我应该使用 Userfactory 还是 CreatePerOwinContext?

这是我的 Startup.Auth.cs 中的内容

public partial class Startup {

public void ConfigureAuth(IAppBuilder app) {

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/"),
Provider = new CookieAuthenticationProvider {
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}

这是我的 WebAPI 配置:

public static class WebApiConfig
{
public static void CustomizeConfig(HttpConfiguration config)
{
config.Formatters.Remove(config.Formatters.XmlFormatter);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
json.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mmZ" });
}

我看到了一些使用此代码的示例,但我不确定如何调用它:

OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};

我可以用这个替换 cookie 身份验证吗?

最佳答案

不是专家,但在我的涉猎中,我发现 token 对于 api 以及从 javascript 到 api 非常有效,而传统的 cookie 主要适用于 ui。任一或两者都将起作用,具体取决于您想要做什么。

您可以点击类似此链接的链接,该链接为 ui 提供 cookie,为 api 提供 token http://blog.iteedee.com/2014/03/asp-net-identity-2-0-cookie-token-authentication/

  app.CreatePerOwinContext(ApplicationSession.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

// Token Authentication
app.UseOAuthBearerAuthentication(new OAuthBearerOptions());

我认为,如果您想要两者都拥有承载,则可以将 cookie 身份验证选项身份验证类型设置为承载,但您必须使用它。该 token 将位于“.AspNet.ExternalBearer”下的owincontext 中。

我还认为,如果您注册 Identity 2.0 中间件,我认为它也会注册 oauth 中间件,因此您不需要自己注册 oauthserver 中间件。这就是您发布的 OAuthAuthorizationServerOptions 代码。你不需要它。

如果 ui 和 api 是分开的,那么如果您想从 ui 传递到 api 进行某种单点登录,那就有点困难了。我建议从 thinktecture 查看开源身份服务器或授权服务器。

如果您设置了owin中间件和Identity 2.0,您需要确保应用程序和api都可以读取 token ,并且您可能需要实现ISecureDataFormat。但请记住,解密并不意味着您可以 100% 信任 token ,它应该经过签名和验证。取决于您的需求。

抱歉,我想这是一个很长的漫谈......祝你好运。

关于asp.net - 我应该如何处理 Identity 2.0、WebAPI 2.1 和 Owin 2 的身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343386/

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