注册用户后,Identity Server 4自动登录

时间:2019-04-21 22:00:12

标签: login asp.net-core-mvc identityserver4 registration

我想知道是否有人知道在Identity Server 4中注册用户后如何“登录”用户?

在当前设置中,我的网站(localhost:44300)上有一个注册向导。在向导过程中,用户将输入一个用户名,电子邮件和密码,然后使用http发布呼叫将其发送到我的IS4服务器(localhost:44310)。在IS4服务器上,我创建用户,然后调用_signManager.SignInAsync。我也尝试过调用HttpContext.SignInAsync。返回我的网站后,用户将完成注册向导,然后将其发送到我们的仪表板。问题是将用户发送到仪表板时,要求他们登录。我认为这与客户端没有收到任何令牌/ cookie有关。

那么有人知道我将如何做到这一点吗? 请注意,我发现的最接近的问题是这个问题,但尚未得到回答:Identity Server 4 Auto Login After Registration

请让我知道我要完成的工作是否很清楚。

谢谢您的时间。

客户端启动

services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";
    options.Authority = "https://localhost:44310/";
    options.ClientId = "Id";
    options.ClientSecret = "Secret";
    options.RequireHttpsMetadata = true;
    options.ResponseType = "code id_token";
    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = false;
    options.SignedOutRedirectUri = "https://localhost:44300/";
    options.Scope.Add("openid");
    options.Scope.Add("profile");
    options.Scope.Add("Identity");
});

客户注册

var disco = await Client.GetDiscoveryDocumentAsync("https://localhost:44310/");
if (disco.IsError)
{
    throw new Exception(disco.Error);
}

var tokenResponse = await Client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
{
    Address = disco.TokenEndpoint,

    ClientId = "Id",
    ClientSecret = "Secret",
    Scope = "Identity"
});

if (tokenResponse.IsError)
{
    throw new Exception(tokenResponse.Error);
}

var token = tokenResponse.AccessToken;
Client.SetBearerToken(tokenResponse.AccessToken);

var myContent = JsonConvert.SerializeObject(new {
    model.Username,
    model.Email,
    model.Password,
    Claims = new List<string> {"User"},
});
var response = await Client.PostAsync("Account/register", new StringContent(myContent, Encoding.UTF8, "application/json"));

return await response.Content.ReadAsAsync<Models.RegistrationResponse>();

IS4寄存器

[Authorize]
[HttpPost]
public async Task<IActionResult> Register([FromBody]RegistrationRequest model)
{ 
    user = new IdentityUser()
    {
        UserName = model.Username,
        Email = model.Email,
    };
    var result = await _userManager.CreateAsync(user, model.Password);

    if (result.Succeeded)
    {
        var cliams = new List<Claim>();

        foreach (var claim in model.Claims)
        {
            cliams.Add(new Claim(claim, ""));
        }
        await _userManager.AddClaimsAsync(user, cliams);

        await _signManager.SignInAsync(user, true);//I was originally just doing this

        //I tried copying what Account/Login does, which is the following lines
        await _events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.UserName));

        await HttpContext.SignInAsync(user.Id, user.UserName);
        ////////////////////////////

        return Ok(new RegistrationResponse()
        {
            Successful = true,
            UserId = user.Id,
        });
    }
}

0 个答案:

没有答案