developer tip

ASP.NET MVC에서 프로필 공급자 구현

copycodes 2020. 11. 25. 08:05
반응형

ASP.NET MVC에서 프로필 공급자 구현


내 평생 동안 나는 SqlProfileProvider가 내가 작업중 인 MVC 프로젝트에서 작동하도록 할 수 없습니다.

내가 깨달은 첫 번째 흥미로운 점은 Visual Studio가 자동으로 ProfileCommon 프록시 클래스를 생성하지 않는다는 것입니다. ProfileBase 클래스를 확장하는 것이 단순하기 때문에 큰 문제는 아닙니다. ProfileCommon 클래스를 만든 후 사용자 프로필을 만들기 위해 다음과 같은 Action 메서드를 작성했습니다.

[AcceptVerbs("POST")]
public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip)
{
    MembershipUser user = Membership.GetUser();
    ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

    profile.Company = company;
    profile.Phone = phone;
    profile.Fax = fax;
    profile.City = city;
    profile.State = state;
    profile.Zip = zip;
    profile.Save();

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

내가 겪고있는 문제는 ProfileCommon.Create ()에 대한 호출이 ProfileCommon 형식으로 캐스팅 할 수 없기 때문에 프로필 개체를 다시 가져올 수 없다는 것입니다. 이로 인해 프로필이 null이기 때문에 분명히 다음 줄이 실패합니다.

다음은 내 web.config의 일부입니다.

<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
    <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
    <properties>
        <add name="FirstName" type="string" />
        <add name="LastName" type="string" />
        <add name="Company" type="string" />
        <add name="Phone" type="string" />
        <add name="Fax" type="string" />
        <add name="City" type="string" />
        <add name="State" type="string" />
        <add name="Zip" type="string" />
        <add name="Email" type="string" >
    </properties>
</profile>

MembershipProvider는 문제없이 작동하므로 연결 문자열이 좋다는 것을 알고 있습니다.

도움이되는 경우를 대비하여 다음은 내 ProfileCommon 클래스입니다.

public class ProfileCommon : ProfileBase
    {
        public virtual string Company
        {
            get
            {
                return ((string)(this.GetPropertyValue("Company")));
            }
            set
            {
                this.SetPropertyValue("Company", value);
            }
        }

        public virtual string Phone
        {
            get
            {
                return ((string)(this.GetPropertyValue("Phone")));
            }
            set
            {
                this.SetPropertyValue("Phone", value);
            }
        }

        public virtual string Fax
        {
            get
            {
                return ((string)(this.GetPropertyValue("Fax")));
            }
            set
            {
                this.SetPropertyValue("Fax", value);
            }
        }

        public virtual string City
        {
            get
            {
                return ((string)(this.GetPropertyValue("City")));
            }
            set
            {
                this.SetPropertyValue("City", value);
            }
        }

        public virtual string State
        {
            get
            {
                return ((string)(this.GetPropertyValue("State")));
            }
            set
            {
                this.SetPropertyValue("State", value);
            }
        }

        public virtual string Zip
        {
            get
            {
                return ((string)(this.GetPropertyValue("Zip")));
            }
            set
            {
                this.SetPropertyValue("Zip", value);
            }
        }

        public virtual ProfileCommon GetProfile(string username)
        {
            return ((ProfileCommon)(ProfileBase.Create(username)));
        }
    }

내가 뭘 잘못하고 있는지에 대한 생각이 있습니까? 나머지 사람들이 ASP.NET MVC 프로젝트에 ProfileProvider를 성공적으로 통합 했습니까?

미리 감사드립니다 ...


수행해야 할 작업은 다음과 같습니다.

1) Web.config 섹션에서 다른 속성 설정 외에 "상속"속성을 추가합니다.

<profile inherits="MySite.Models.ProfileCommon" defaultProvider="....

2) <properties>Web.config에서 전체 섹션을 제거 합니다. 사용자 지정 ProfileCommon 클래스에서 이미 정의했고 이전 단계에서 사용자 지정 클래스에서 상속하도록 지시 했으므로

3) ProfileCommon.GetProfile () 메서드의 코드를 다음과 같이 변경하십시오.

public virtual ProfileCommon GetProfile(string username)        
{            
     return Create(username) as ProfileCommon;      
}

도움이 되었기를 바랍니다.


전체 질문에 대해서는 확실하지 않지만 코드에서 한 가지 눈에 띄었습니다.

ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;

You do not need both the (ProfileCommon) and the as ProfileCommon. They both do casts, but the () throws and exception while the as returns a null if the cast can't be made.


Try Web Profile Builder. It's a build script that automagically generates a WebProfile class (equivalent to ProfileCommon) from web.config.


The web.config file in the MVC Beta is wrong. The SqlProfileProvider is in System.Web.Profile, not System.Web.Security. Change this, and it should start working for you.

참고URL : https://stackoverflow.com/questions/79129/implementing-profile-provider-in-asp-net-mvc

반응형