Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.Net Core 2.x中结合使用IdentityServer 4和标准MVC身份验证_C#_Asp.net_Asp.net Mvc_Asp.net Core_Identityserver4 - Fatal编程技术网

C# 在.Net Core 2.x中结合使用IdentityServer 4和标准MVC身份验证

C# 在.Net Core 2.x中结合使用IdentityServer 4和标准MVC身份验证,c#,asp.net,asp.net-mvc,asp.net-core,identityserver4,C#,Asp.net,Asp.net Mvc,Asp.net Core,Identityserver4,我们有各种各样的内部、外部和移动应用程序,这些应用程序导致我们的用户必须在整个组织内反复登录。我一直在研究一个过程,该过程允许我们使用IdentityServer4在所有不同的工具上实现单点登录,但在这个过程中我遇到了很多问题 在我们的传统webforms应用程序中,用户通过会话/形式验证登录。在我们新的MVC核心应用程序中,我们使用内置的MVC身份验证(而不是身份验证)。在移动应用程序中,我们登录到遗留应用程序,并随每个请求一起传递会话信息。最后,在我们的第三方应用程序中,它使用windows

我们有各种各样的内部、外部和移动应用程序,这些应用程序导致我们的用户必须在整个组织内反复登录。我一直在研究一个过程,该过程允许我们使用IdentityServer4在所有不同的工具上实现单点登录,但在这个过程中我遇到了很多问题

在我们的传统webforms应用程序中,用户通过会话/形式验证登录。在我们新的MVC核心应用程序中,我们使用内置的MVC身份验证(而不是身份验证)。在移动应用程序中,我们登录到遗留应用程序,并随每个请求一起传递会话信息。最后,在我们的第三方应用程序中,它使用windows身份验证进行登录

我们正在将所有东西移植到新的应用程序中,但随着这些东西的发展,所有东西都在分块移动。与继续在遗留应用程序中实现功能不同,我们希望在新平台中实现某种连接身份验证,从而允许我们向旧系统和新系统发出请求。这就是我偶然发现IdentityServer的原因

目前,我正试图弄清楚如何使用IdentityServer授权移动应用程序中的用户,并让他们完全访问项目的端点,但同时我想确保我们不会对现有的授权和角色系统产生不利影响

我遇到的一个大问题是,如果我对IdentityServer应用最小配置,但在my startup.cs中添加
#UseIdentityServer
以及
#UseAuthentication
,则登录时会收到错误

这是否意味着我必须重写当前的授权算法以全面使用identity server,或者我是否可以将其调整为与当前系统一起使用

下面是一些未实现IdentityServer的现有代码

//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    // Cookie authentication changed in .Net Core 2.x, we have to configure everything here.
    services.AddAuthentication(SecurityConstants.CookieAuthenticationScheme)
            .AddCookie(SecurityConstants.CookieAuthenticationScheme, o =>
            {
                o.LoginPath = "/Account/Login";
                o.LogoutPath = "Account/LogOff";
                o.AccessDeniedPath = "/Home/Error";
                o.SlidingExpiration = true;
                o.Cookie = new CookieBuilder
                {
                    Name = SecurityConstants.CookieAuthenticationScheme,
                    Expiration = TimeSpan.FromMinutes(SecurityConstants.CookieExpirationMinutes),
                    HttpOnly = true
                };
            });
    services.AddAuthentication(o =>
    {
        o.DefaultChallengeScheme = SecurityConstants.CookieAuthenticationScheme;
        o.DefaultSignInScheme = SecurityConstants.CookieAuthenticationScheme;
        o.DefaultAuthenticateScheme = SecurityConstants.CookieAuthenticationScheme;
    });

    // Other standard MVC setup junk removed.
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAuthentication();
    app.UseSession();

    // routing and irrelevant logic removed
}
帐户管理员登录

[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(string userName, string password, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    var authResult = Authentication.Authenticate(userName, password);
    if (authResult.IsAuthenticated)
    {
        var p = new BaseClaimsPrincipal(authResult.AuthenticatedUser);

        await HttpContext.SignInAsync(SecurityConstants.CookieAuthenticationScheme, new ClaimsPrincipal(p));

        return RedirectToLocal(returnUrl);
    }

    ViewBag.Error = "Incorrect Username or Password";

    return View(true);
}
[HttpPost]
[异名]
公共异步任务登录(字符串用户名、字符串密码、字符串返回URL=null)
{
ViewData[“ReturnUrl”]=ReturnUrl;
var authResult=Authentication.Authenticate(用户名、密码);
如果(authResult.IsAuthenticated)
{
var p=新的BaseClaimsPrincipal(authResult.AuthenticatedUser);
等待HttpContext.SignInAsync(SecurityConstants.CookieAuthenticationScheme,newclaimsprincipal(p));
返回重定向到本地(returnUrl);
}
ViewBag.Error=“用户名或密码不正确”;
返回视图(true);
}

上面的
BaseClaimsPrincipal
只创建了一个
genericientity
ClaimsIdentity
供登录过程使用。

一个小注释,
UseIdentityServer
包含对
UseAuthentication
的内部调用,如果/当您解决问题时,您不需要这两个小注释,
UseIdentityServer
包含对内部
UseAuthentication
的调用,如果/当您解决问题时,您不需要这两个调用。