Use Authentication in the wcf ria Service for custom Authentication

Source: Internet
Author: User
Tags silverlight

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.