.net web api spa - 访问GetUserInfo中的AuthenticationProperties

时间:2014-05-06 06:31:03

标签: asp.net asp.net-web-api single-page-application owin

我使用Knockout SPA VS模板创建了一个新项目。

AccountController中的

有一个方法GetExternalLogin,其末尾有以下代码:

Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user, OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await UserManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);

我修改了方法CreateProperties以返回一些额外的数据:

IDictionary<string, string> data = new Dictionary<string, string>
{
    { "userId", "1" },
    { "userName", userName },
    { "isActivated", "false" },
    { "accountType", "2" }
};
return new AuthenticationProperties(data);

这不是敏感数据,我希望只要调用操作GetUserInfo(),这些数据就可用。

[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
[Route("UserInfo")]
public UserInfoViewModel GetUserInfo()
{
    ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

    return new UserInfoViewModel
    {
        UserName = User.Identity.GetUserName(),
        HasRegistered = externalLogin == null,
        LoginProvider = externalLogin != null ? externalLogin.LoginProvider : null
        // I want to add custom data here
    };
}

假设我能以某种方式检索该方法中的数据,我错了吗?

0 个答案:

没有答案