문제
멤버십 클래스를 사용하여 나만의 가입 양식을 작성하고 사용자를 생성했습니다.
MembershipCreateStatus status;
MembershipUser newUser = Membership.CreateUser(tbxUsername.Text,
tbxPassword.Text,
tbxEmail.Text,
null, null, true, out status);
코드를 사용하여 사용자가 생성 된 후에는 다음과 같은 프로필 속성을 설정하려고합니다.
Profile.CountryCode = ddlCountry.SelectedValue;
Profile.DisplayName = tbxDisplayName.Text;
Profile.Save();
그러나 나는 다음과 같은 예외 메시지를 얻습니다
익명 사용자에게는이 속성을 설정할 수 없습니다.
내가 이것을 얻는 이유는 무엇입니까?
해결책
나는 이것이 당신이 프로필을 먼저 가져 오지 않은 Beacuse라고 생각합니다 (DB/당신이 사용하고있는 것).
코드는 다음과 같습니다.
ProfileCommon p = Profile.GetProfile(tbxUsername.Text);
p.CountryCode = ddlCountry.SelectedValue;
p.DisplayName = tbxDisplayName.Text;
p.Save();
다른 팁
나는 같은 오류에 부딪쳤다. 그리고 그것은 authcookie 만 설정했지만 httpcontext 사용자를 설정하지 않았기 때문입니다. 보다:
HttpCookie authCookie = HttpContext.CurrentRequest.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
//Extract the forms authentication cookie
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
// If caching roles in userData field then extract
string[] roles = authTicket.UserData.Split(new char[] { '|' });
// Create the IIdentity instance
IIdentity id = new FormsIdentity(authTicket);
// Create the IPrinciple instance
IPrincipal principal = new GenericPrincipal(id, roles);
// Set the context user
HttpContext.Current.User = principal;
}
특별히 감사함 이 게시물 코드를 위해!
제휴하지 않습니다 StackOverflow