ASP.NETIdentity的中央登录/身份验证应用程序用于两个分开的ASP.NETMVC应用程序。
创始人
2024-09-19 00:01:13
0

使用ASP.NET Identity创建中央身份验证服务,用于身份验证并共享用户凭据。在两个分别的ASP.NET MVC应用程序中,配置以使用相同的中央身份验证服务进行身份验证。

例如,我们可以创建一个名为"CentralAuth"的ASP.NET Identity项目,并配置其作为中央身份验证服务。然后,在两个分别的ASP.NET MVC应用程序中,使用以下代码配置身份验证:

// 在Startup.Auth.cs文件中

app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(ApplicationSignInManager.Create);

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
   AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
   LoginPath = new PathString("/Account/Login"),
   Provider = new CookieAuthenticationProvider
   {
      OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(
      validateInterval: TimeSpan.FromMinutes(30),
      regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
   }
});

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

app.UseCentralizedOAuth(new CentralizedOAuthOptions()){
    AuthenticationMode = AuthenticationMode.Passive,
    ClientId = "myAppId",
    ClientSecret = "myAppSecret"
});

// 在Web.Config文件中



然后,在两个应用程序中定义UseCentralizedOAuth方法:

// 在Controllers/AccountController.cs文件中

public ActionResult ExternalLoginCallback(string returnUrl)
{
   var ctx = Request.GetOwinContext();
   var authManager = ctx.Authentication;

   var loginInfo = authManager.GetExternalLoginInfoAsync().Result;
   if (loginInfo == null)
   {
      return RedirectToAction("Login");
   }

   var userManager = HttpContext.GetOwinContext().GetUserManager();
   var user = userManager.FindAsync(loginInfo.Login).Result;
   if (user != null)
   {
      authManager.SignIn(new AuthenticationProperties { IsPersistent = false }, user.GenerateUserIdentityAsync

相关内容

热门资讯

透视辅助!wepoker软件辅... 透视辅助!wepoker软件辅助程序,wepoker怎么设置房间-都是是真的有辅助脚本(哔哩哔哩)一...
透视了解!wepoker作弊视... 透视了解!wepoker作弊视频,wepoker俱乐部辅助-都是有辅助脚本(哔哩哔哩)1、完成辅助器...
透视有挂!aapoker如何设... 透视有挂!aapoker如何设置胜率,aapoker免费透视脚本-都是有辅助插件(哔哩哔哩)1、游戏...
透视揭露!wepoker开辅助... 透视揭露!wepoker开辅助能查到吗,wepoker有插件吗-一贯一直总是有辅助攻略(哔哩哔哩)1...
透视分享!wpk辅助插件,wp... 透视分享!wpk辅助插件,wpk有那种辅助吗-原来一直总是有辅助工具(哔哩哔哩)1、超多福利:超高返...
透视科普!wepoker免费脚... 透视科普!wepoker免费脚本咨询,wepoker辅助透视-其实存在有辅助插件(哔哩哔哩)1、用户...
透视解迷!大菠萝789辅助器下... 透视解迷!大菠萝789辅助器下载,cloudpoker作弊-真是存在有辅助攻略(哔哩哔哩)1、大菠萝...
透视科普!wepoker辅助器... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视必备!wepoker插件辅... 透视必备!wepoker插件辅助,wepoker有透视底牌吗-真是有辅助教程(哔哩哔哩)1、让任何用...
透视必备!pokerworld... 透视必备!pokerworld修改器,悦扑克脚本-本来真的有辅助工具(哔哩哔哩)1、pokerwor...