Asp.net Web表单,天门冬氨酸身份 - 如何存储来自Facebook,Twitter等索赔 [英] Asp.net web forms, Asp Identity - how to store claims from Facebook, Twitter, etc

查看:253
本文介绍了Asp.net Web表单,天门冬氨酸身份 - 如何存储来自Facebook,Twitter等索赔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此请求是基于新的Visual Studio 2013集成Asp.net身份的东西。我已经看到了一些关于这个问题的MVC的职位,但对我的生活不能得到它的标准Web窗体工作。我试图做的是从我回来从Facebook(或其他服务)的索赔填充AspNetUserClaims表。我居然能看到值回来在下面的OnAuthenticated,但不能为我的生活弄清楚如何将这些要求加入到在当前登录用户的上下文?



有数以百计的MVC例子围绕这个,但很可惜没有Web形式的例子。这应该是完全明了,但由于某些原因,我不能匹配的用户的索赔和凭据从Facebook回来了当前登录用户的上下文。



目前的OnAuthenticated火灾后,它显然返回我的页面(RegisterExternalLogin.aspx)作为内置的例子提供。然而,权利要求书都不见了,登录到Facebook的上下文中消失了,我不能做任何事情,在这一点别的。



所以,最终的问题是,一个人如何填充从Facebook索赔成基于在当前登录用户的上下文WITHOUT使用MVC的AspNetUserClaims表?

  VAR fboptions =新FacebookAuthenticationOptions(); 
fboptions.AppId =xxxxxxxxxxxxxxxxxxx;
fboptions.AppSecret =yyyyyyyyyyyyyyyyyyyyyy;
fboptions.Scope.Add(电子邮件);
fboptions.Scope.Add(friends_about_me);
fboptions.Scope.Add(friends_photos);

fboptions.Provider =新FacebookAuthenticationProvider()
{
OnAuthenticated =(上下文)=>
{
的foreach(在context.User变种V)
{
context.Identity.AddClaim(新System.Security.Claims.Claim(v.Key,v.Value。的ToString()));
}
context.Identity.AddClaim(新System.Security.Claims.Claim(FacebookAccessToken,context.AccessToken));
返回Task.FromResult(0);
},
};
app.UseFacebookAuthentication(fboptions);


解决方案

好吧,我知道你说你想知道如何做无MVC,虽然我不能直接回答,我可以告诉你,我有类似的问题与MVC,如何我解决了它。 。希望这点你在Web表单正确的方向



我想向您推荐的博客文章中,我在的 http://www.beabigrockstar.com/get-the-twitter-轮廓图像,使用最ASP净身份/



关键位是以下内容:



在您登录使用您定向到您注册新用户的页面外部供应商。回发后从我不得不添加一行首先再次获取ClaimsIdentity:

  ClaimsIdentity claimsIdentity = 
等待AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);



在不调用这一行上面,我没有在所有的工作,所以也许这就是你的问题所在。



有一次,我就可以通过对身份的索赔财产索赔的集合迭代ClaimsIdentity。这应该包括令牌您在OnAuthenticated回调委托添加了Facebook。所有您需要做的就是调用UserManager.AddClaimAsync()将其添​​加到索赔数据库表。



有关更多信息看我的AccountController类在的 https://github.com/beabigrockstar/AspNetIdentitySocialProfileImage/blob/master/Controllers/AccountController.cs



在方法ExternalLoginConfirmation(ExternalLoginConfirmationViewModel型号,串RETURNURL),并调用StoreAuthTokenClaims()专门研究,然后,当然这个过程中我按照StoreAuthTokenClaims ()



希望这个能以某种方式帮助你在的WebForms。


This request is based upon the new Visual Studio 2013 integration of Asp.net Identity stuff. I have seen some of the posts regarding this question for MVC, but for the life of me cannot get it to work for standard Web Forms. What I'm trying to do is populate the AspNetUserClaims table from the claims that I get back from Facebook (or other service). I actually can see the values coming back in the OnAuthenticated below, but cannot for the life of me figure out how to add these claims to the context of the currently logged in user?

There are literally hundreds of MVC examples surrounding this, but alas no Web Forms examples. This should be completely straightforward, but for some reason I cannot match up the context of the currently logged in user to the claims and credentials coming back from Facebook.

Currently after the OnAuthenticated fires, it obviously returns me to the page (RegisterExternalLogin.aspx) as the built-in example provides. However, the claims are gone, the context of the login to Facebook is gone, and I can't do anything else at this point.

So the ultimate question is, HOW does one populate the claims FROM Facebook into the AspNetUserClaims table based upon the context of the currently logged in user WITHOUT using MVC?

        var fboptions = new FacebookAuthenticationOptions();
        fboptions.AppId = "xxxxxxxxxxxxxxxxxxx";
        fboptions.AppSecret = "yyyyyyyyyyyyyyyyyyyyyy";
        fboptions.Scope.Add("email");
        fboptions.Scope.Add("friends_about_me");
        fboptions.Scope.Add("friends_photos");

        fboptions.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = (context) =>
            {
                foreach (var v in context.User)
                {
                    context.Identity.AddClaim(new System.Security.Claims.Claim(v.Key, v.Value.ToString()));
                }
                context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                return Task.FromResult(0);
            },
        };
        app.UseFacebookAuthentication(fboptions);

解决方案

Ok, I know you said you want to know how to do WITHOUT MVC, and while I cannot answer that directly, I can tell you that I had similar issues with MVC, and how I resolved it. Hopefully that points you in the right direction in Web Forms.

I would like to refer you to the blog article I wrote about it at http://www.beabigrockstar.com/get-the-twitter-profile-image-using-the-asp-net-identity/

The key bits was the following:

After you have logged in using the external provider you are directed to a page where you register the new user. After the postback from that I had to add a line to first of all get the ClaimsIdentity again:

ClaimsIdentity claimsIdentity =
            await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

Without the call to this line above it did not work for me at all, so maybe that is where your problem lies.

Once I had the ClaimsIdentity you can iterate through the collection of claims on the Claims property of the identity. This should include the Facebook token you added in the OnAuthenticated callback delegate. All you have to do then is call UserManager.AddClaimAsync() to add it to the Claims database table.

For more info look at my AccountController class at https://github.com/beabigrockstar/AspNetIdentitySocialProfileImage/blob/master/Controllers/AccountController.cs

Look specifically at the method ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl) and the call to StoreAuthTokenClaims(), and then of course the process I follow in StoreAuthTokenClaims()

Hope that this can somehow help you in WebForms.

这篇关于Asp.net Web表单,天门冬氨酸身份 - 如何存储来自Facebook,Twitter等索赔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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