如何将信息从OnAuthenticated事件传递到控制器和登录? [英] How to pass information from OnAuthenticated event to a controller and SignIn?

查看:78
本文介绍了如何将信息从OnAuthenticated事件传递到控制器和登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里找到了,所以我无法打电话

As I've found out here, I can't call

HttpContext.GetOwinContext().Authentication.SignIn(...)

FacebookAuthenticationProvider OnAuthenticated事件中的

.似乎是另外一种情况.

in FacebookAuthenticationProvider OnAuthenticated event. It seems to be a different context.

我的用户将来自Facebook,这意味着我将需要检索诸如idemail等数据....可以在OnAuthenticated事件上访问此信息,并且需要此信息才能登录.

My users will be from Facebook, which means that I will need to retrieve data such as id, email... This information is accessible on the OnAuthenticated event and I need this information to SignIn.

我需要在我的控制器上访问此信息...

I need to access this information on my controller...

我在事件中尝试过此操作:

I tried this in the event:

context.OwinContext.Set("FacebookUser", rawUserObjectFromFacebookAsJson);

但是在控制器上,如果我尝试检索它,则为空.

But on the controller if I try to retrieve this, it is null.

var fbuser = HttpContext.GetOwinContext()
                 .Get<Newtonsoft.Json.Linq.JObject>("FacebookUser");

所以我的问题是,如何将这些数据传递给控制器​​以便登录?

So my question is, how can I pass this data to the controller so I can sign in?

推荐答案

我通过查看此答案找到了一种 /a>并弄清楚我能做什么.

I found a way to do this by looking at this answer and figuring out what I could do.

使用Claims,我可以添加从Facebook检索到的所有值并将其放入身份声明中.

Using Claims I can add all values retrieved from Facebook and throw into identity claims.

OnAuthenticated = (context) =>
{
    const string XmlSchemaString = "http://www.w3.org/2001/XMLSchema#string";

    var rawUserObjectFromFacebookAsJson = context.User;

    context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, XmlSchemaString, "Facebook"));
    foreach (var x in context.User)
    {
        var claimType = string.Format("urn:facebook:{0}", x.Key);
        string claimValue = x.Value.ToString();
        if (!context.Identity.HasClaim(claimType, claimValue))
            context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, XmlSchemaString, "Facebook"));

    }

    return Task.FromResult(0);
}

然后在我的控制器上,我可以使用此身份

Then on my controller I can get that identity by using this

ClaimsIdentity identity = await HttpContext.GetOwinContext().Authentication
    .GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

然后我将采取行动

[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    ClaimsIdentity identity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

    var user = new IdentityUser()
    {
        Id = identity.GetUserId(),
        UserName = identity.Name,
    };

    await LoginAsync(user, identity);

    if (!identity.IsAuthenticated)
    {
        return RedirectToAction("Login");
    }

    return RedirectToAction("Index", "Home");
}

还有我的LoginAsync方法

And my LoginAsync method

private async Task LoginAsync(IdentityUser user, ClaimsIdentity identity)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

    // I can't just use the identity I got on Facebook
    // I need to create this one, or else it will not signin properly
    // The authentication type has to be ApplicationCookie and the property
    // is readonly, so...
    var userIdentity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

    // now we have to transfer the claims, adding a check to avoid duplicates
    foreach (var claim in identity.Claims)
    {
        if (!userIdentity.HasClaim(c => c.Type == claim.Type))
            userIdentity.AddClaim(claim);
    }

    // then it will signin successfully
    AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity);
}

然后我可以访问

HttpContext.GetOwinContext().Authentication.User.Claims

随时

找回我需要的东西.

at any time and retrieve what I need.

这篇关于如何将信息从OnAuthenticated事件传递到控制器和登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆