【问题标题】:OpenId update profile informationOpenId 更新配置文件信息
【发布时间】:2011-04-21 05:49:57
【问题描述】:

使用 OpenId 跟踪站点中当前用户的推荐解决方案是什么?假设我有一个带有 id 和声明标识符的用户表,然后是我的站点特定信息,并且用户想要更新他们的站点特定信息。鉴于我没有使用内置会员资格,跟踪当前用户的最佳方式是什么?每次用户尝试更新他们的个人资料时,我是否应该向 openid 发送请求以获取 ClaimedIdentifier?或者也许只是强制用户名是唯一的并根据用户名获取用户信息?

【问题讨论】:

    标签: asp.net-mvc openid dotnetopenauth


    【解决方案1】:

    我是用 cookie 做的 :)...你可能会发现我的回答很有用:What OpenID solution is really used by Stack Overflow?

    我还写了一篇关于它的简单博文:http://codesprout.blogspot.com/2011/03/using-dotnetopenauth-to-create-simple.html

    public class User
    {
        [DisplayName("User ID")]
        public int UserID{ get; set; }
    
        [Required]
        [DisplayName("Open ID")]
        public string OpenID { get; set; }
    
        [DisplayName("User Name")]
        public string UserName{ get; set; }
    }
    

    在我的示例中,我使用 OpenID 登录并将其存储在 cookie 中,但您可以在 cookie 中存储其他信息,例如用户名:

    public class FormsAuthenticationService : IFormsAuthenticationService
    {
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(serName)) throw new ArgumentException("The user name cannot be null or empty.", "UserName");
    
            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }
    
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
    }  
    

    更新 2.0:
    像这样的东西怎么样(这是视图):

    <%
        if (Request.IsAuthenticated) 
        {
            string name = Request.Cookies[Page.User.Identity.Name] == null ? string.Empty : Request.Cookies[Page.User.Identity.Name].Value;
            if (string.IsNullOrEmpty(name))
            {
                name = Page.User.Identity.Name;
            }
    %>
    
            [<%: Html.ActionLink(name, "Profile", "User")%> |
             <%: Html.ActionLink("Log out", "LogOut", "User") %> |
    <%
        }
        else 
        {
    %> 
            [ <%: Html.ActionLink("Log in", "LogIn", "User") %> |
    <%
        }
    
    %>
    

    和控制器,大概你在登录后被带到一个Profile页面(或者你可以在LogIn方法中设置Response.Cookies)并且当你加载模型时你设置在 cookie 中显示名称:

        [Authorize]
        [HttpGet]
        public ActionResult Profile(User model)
        {
            if (User.Identity.IsAuthenticated)
            {
                userRepository.Refresh();
                model = userRepository.FetchByOpenID(User.Identity.Name);
    
                // If the user wasn't located in the database
                // then add the user to our database of users
                if (model == null)
                {
                    model = RegisterNewUser(User.Identity.Name);
                }
    
                Response.Cookies[model.OpenID].Value = model.DisplayName;
                Response.Cookies[model.OpenID].Expires = DateTime.Now.AddDays(5);
    
                return View(model);
            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
    

    您可以在我的一个小项目中看到这一切:mydevarmy。我将很快发布用户个人资料,您将能够更改显示名称(现在是自动生成的)。

    【讨论】:

    • 同意。尽管没有使用 ASP.NET Membership,但您仍然可以而且应该使用 FormsAuthentication 登录 cookie 来记住登录的用户。
    • @Andrew,来自 THE MAN 本人!我觉得我做对了 :)... 无论如何,谢谢你给了我们 DotNetOpenAuth:它让我的生活更轻松,作为回报,我到处都在实施它!
    • @Lirik,很酷,这与我所拥有的非常相似,所以我认为我在正确的轨道上。虽然我在尝试更新他的个人资料信息时如何获取当前用户有点卡住了。您用于设置 cookie 的名称是一个友好名称,所以不能有多个用户使用该友好名称吗?或者您是否使用声明的 id,如果是,您是否必须使用会话状态来获取友好名称?
    • @Joe,您可以将友好名称设置为您想要的任何名称...即使用户共享相同的友好名称,据我所知,他们不会共享相同的 cookie应该不是问题。如果您的服务器依赖于唯一的友好名称,那么您可以强制使用唯一的友好名称(即使用UserID 而不是UserName)。如果您需要找到UserName,那么您可以通过UserID 从您的数据库中查找它。
    • @Lirik,我明白了。在一个完美的世界中,我希望能够简单地使用 User.Identity.Name 快速显示友好名称,但随后能够使用声明的 id 或我想的用户 id 获取当前用户的个人资料信息。我倾向于将两者都放入 cookie 中,因为这似乎是获得这种行为的唯一方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多