MVC.NET Core 2自定义登录控制器

时间:2018-10-09 12:44:47

标签: authentication asp.net-core-2.1

是否可以在MVC“客户端”(.net核心2)中创建自定义登录表单 通过从身份验证服务器(身份服务器4)发出令牌并将令牌/凭据设置到MVC管道进行授权?

身份验证服务器:

new Client{
ClientId = "MVC",
ClientName = "MVC",
RequireClientSecret = true,

AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
 ClientSecrets = {
    new Secret("secret".Sha256())
},

 AllowedScopes = {
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile,
    "roles",
    configuration["AUTHENTICATION_SCOPE:SCOPE_ID"],
},

AllowOfflineAccess = true,
AlwaysSendClientClaims = true,
AlwaysIncludeUserClaimsInIdToken = true,
AccessTokenType = AccessTokenType.Reference,
AccessTokenLifetime = int.Parse(configuration["AccessTokenLifetime"]), 
AbsoluteRefreshTokenLifetime = int.Parse(configuration["AbsoluteRefreshTokenLifetime"])}

MVC客户端:

Startup.cs

public void ConfigureServices(IServiceCollection services){
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();


services.AddAuthentication(options =>
{
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;

}).AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

    options.Authority = "...";
    options.RequireHttpsMetadata = false;

    options.GetClaimsFromUserInfoEndpoint = true;

    options.ClientId = "MVC";
    options.ClientSecret = "secret";
    options.ResponseType = "code id_token";

    options.SaveTokens = true;
}).AddCookie(options =>
{
    options.LoginPath = new PathString("/Account/Login/");
    options.LogoutPath = new PathString("/Account/Logout/");
    options.AccessDeniedPath = new PathString("/Account/Login/");
});}

AccountController.cs

[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel vm, string button){
if (!ModelState.IsValid)
    return View(vm);

//HOW TO CONTINUE FROM HERE?    
//Issue token from auth server and set it in the HttpContext.Authentication?}

0 个答案:

没有答案
相关问题