Many articles have mentioned how to use the verification function in the wcf ria Service, but generally it starts with creating a SilverLight Business Application project. However, the template of the SilverLight Business Application is not so realistic, by default, a bunch of classes and methods are added. It is really disgusting to adjust them based on this, so I tried to use them in a common SilverLight project. The following is a summary:
1. Create a common SilverLight project and select "Enable wcf ria Service". Assume that the project SL project is named MyAuthentication and the corresponding WEB project is MyAuthentication. Web;
2. In MyAuthentication. add an entry to the Web project: Authentication Domain Service. Using the "Authentication Domain Service" project file template, a class inherited from AuthenticationBase <T> is automatically added, and a User class inherited from UserBase is added, assume that the class inherited from AuthenticationBase <T> is named AuthenticationDomainService;
3. Compile the solution. After compilation is successful, classes such as AuthenticationDomainContext, WebContext, and User will be generated in the SL project;
Then you can perform the verification function. You can use the ValidateUser method in the AuthenticationDomainService class to perform custom verification. You can use the GetAuthenticatedUser method to load the information of the currently logged on user.
4. Extend the User class and reload the ValidateUser and GetAuthenticatedUser methods:
View Code
[EnableClientAccess]
Public class AuthenticationDomainService: AuthenticationBase <User>
{
// To enable Forms/Windows Authentication for the Web Application, edit the appropriate section of web. config file.
Protected override bool ValidateUser (string userName, string password)
{
Return userName = "zhangsan" & amp; password = "123456 ";
}
Protected override User GetAuthenticatedUser (IPrincipal principal)
{
Return new User () {Name = principal. Identity. Name, DisplayName = "James "};
// Return base. GetAuthenticatedUser (principal );
}
}
Public class User: UserBase
{
// NOTE: Profile properties can be added here
// To enable profiles, edit the appropriate section of web. config file.
// Public string MyProfileProperty {get; set ;}
Public User ()
: Base ()
{
}
Public string DisplayName {get; internal set ;}
}
After compilation, it seems that you can use WebContext In the SL project. You also need to make the following preparations:
5. Add the WebContext initialization code to the App class constructor in SL:
View Code
WebContext webContext = new WebContext();
webContext.Authentication = new FormsAuthentication();
//webContext.Authentication = new WindowsAuthentication();
this.ApplicationLifetimeObjects.Add(webContext);
6. Specify the Authentication mode as Forms authentication in Web. Config:
<Authentication mode = "Forms">
7. test:
View Code
Private void button#click (object sender, RoutedEventArgs e)
{
LoginOperation operation = WebContext. Current. Authentication. Login (GetLoginParameters (), LoginCompeted, null );
}
Private LoginParameters GetLoginParameters ()
{
Return new LoginParameters ("zhangsan", "123456 ");
}
Private void LoginCompeted (LoginOperation operation)
{
If (operation. LoginSuccess)
{
MessageBox. Show ("success ");
MessageBox. Show (WebContext. Current. User. DisplayName );
}
Else if (operation. HasError)
{
MessageBox. Show (operation. Error. Message );
Operation. MarkErrorAsHandled ();
}
Else if (! Operation. IsCanceled)
{
MessageBox. Show ("incorrect user name or password! ");
}
}
References: http://msdn.microsoft.com/zh-cn/library/ee707361%28v=vs.91%29.aspx