如何在 Xamarin 中快速集成 Android 版认证服务 - 邮箱地址篇
<p style="font-weight: 400">Xamarin 作为微软提供的移动服务多系统开发平台,成为很多开发者首选的应用开发平台。AppGallery Connect(以下简称 AGC)也在逐步的支持 Xamarin 的 SDK。认证服务也是支持 Xamarin 平台的服务之一,今天就教大家如何在 Xamarin 里快速集成认证服务的邮箱地址认证。</p><p>1. 安装 Xamarin 环境</p>
<p style="font-weight: 400">Xamarin 的御用开发平台是 Visual Studio,所以我们需要首先安装 Visual Studio 2019.</p>
<p style="font-weight: 400">下载地址:https://visualstudio.microsoft.com/zh-hans/downloads/</p>
<p style="font-weight: 400">下载好后进行安装,我们需要安装 Xamarin 配套的插件,Mobile development with .NET,中文叫“使用 .NET 的移动开发”,安装完成后就可以通过 Visual Studio 进行 Xamarin 的 Android 和 iOS 开发了。</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180624125-744256016.png"></p>
<p>2. 在 AGC 页面开通认证服务,具体创建与配置骤可以参考认证服务官方文档:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922</p>
<p>3. 创建 Xamarin 工程</p>
<p style="font-weight: 400">在 Visual Studio 中创建新项目,选择移动应用(Xamarin.Forms),将应用的名称等信息设置好后,创建项目</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180658615-1228499189.png"></p>
<p>4. 将远程配置的 Xamarin 包集成到新的项目中</p>
<p style="font-weight: 400">有两种方式集成 Xamarin 包</p>
<p style="font-weight: 400">第一种是云端方式集成,在目录栏右击选择管理 NuGet 程序包</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180817443-953646892.png"></p>
<p style="font-weight: 400">在浏览选项中搜索 Huawei.Agconnect.Auth,点击安装即可</p>
<p style="font-weight: 400">第二种方式是本地集成 NuGet 包,首先需要将所有需要的 NuGet 包放入本地的一个文件夹中,</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180840621-1238022395.png"></p>
<p style="font-weight: 400">还是按照第一种的方式打开 NuGet 包管理器,中岛程序包源边上的设置图标,打开选项目录</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180854729-1970130615.png"></p>
<p style="font-weight: 400">点击绿色的加号添加新的包源,在源的地方输入刚刚存放 NuGet 本地包的文件夹目录即可。</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215180918890-504860536.png"></p>
<p>5. 添加 AGC 配置文件</p>
<p style="font-weight: 400">AGC 为开发者准备了一个存放所有应用相关信息的配置 json 文件,我们需要将这个文件集成到项目中以便后续调用接口时使用,我们的 SDK 会自动读取里面的内容,无需开发者调用时手动传入了,具体步骤如下</p>
<p>a). 按照第二步文档中的方法从 AGC 管理台中下载 agconnect-services.json 文件,将文件放入项目的 Assets 目录下</p>
<p><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215181015512-2100801662.png"></p>
<p>b). 在项目中创建一个新的类,cs,将如下代码写入进行 json 文件的内容读取</p>
<pre class="language-csharp"><code>using System;
using System.IO;
using Android.Util;
using Android.Content;
using Huawei.Agconnect.Config;
namespace XamarinHmsRemoteConfig
{
class HmsLazyInputStream : LazyInputStream
{
public HmsLazyInputStream(Context context) : base(context)
{
Get(context);
}
public override Stream Get(Context context)
{
try
{
return context.Assets.Open("agconnect-services.json");
}
catch (Exception e)
{
Log.Error(e.ToString(), "Can't open agconnect file");
return null;
}
}
}
}</code></pre>
<p>在主 Activity 中,我们需要在 AttachBaseContext 中添加如下代码已读取 json 文件中的内容</p>
<pre class="language-csharp"><code>protected override void AttachBaseContext(Context context)
{
base.AttachBaseContext(context);
AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(context);
config.OverlayWith(new HmsLazyInputStream(context));
}</code></pre>
<p>c). 设置包名</p>
<p style="font-weight: 400">右击项目找到属性选项,在 Android 清单页签找到程序包名称,设置应用的包名。</p>
<p style="font-weight: 400"><img src="https://img2022.cnblogs.com/blog/1969374/202202/1969374-20220215181147644-2140511592.png"></p>
<p style="font-weight: 400">准备工作完成后,我们就可以正式进入开发阶段</p>
<p style="font-weight: 400">对于邮箱地址认证,认证服务 SDK 提供了两个阶段的操作,一个是注册,另一个是登录。这两个操作都需要用到获取邮件验证码的功能,所以认证服务 SDK 也提供了发送验证码的功能:</p>
<p>1. 发送验证码的步骤如下:</p>
<p style="font-weight: 400">首先我们需要创建一个 VerifyCodeSettings 对象,里面包含了发送邮件的相关设置,包括 action,语言等</p>
<pre class="language-csharp"><code>VerifyCodeSettings settings = VerifyCodeSettings.NewBuilder()
.Action(VerifyCodeSettings.ActionRegisterLogin)
.SendInterval(30)
.Locale(Locale.English)
.Build();</code></pre>
<p>读取用户输入的邮箱地址,调用 RequestVerifyCodeAsync 方法向认证服务服务器请求发送验证码,传入邮箱地址以及刚刚我们创建的 setting 对象</p>
<pre class="language-csharp"><code>string email = edtAccount.Text.ToString().Trim();
try
{
var requestVerifyCode = AGConnectAuth.Instance.RequestVerifyCodeAsync(email, settings);
VerifyCodeResult verifyCodeResult = await requestVerifyCode;
if (requestVerifyCode.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
{
Toast.MakeText(this, "Send email verify code success! ", ToastLength.Short).Show();
}
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
}</code></pre>
<p style="font-weight: 400">2. 用户获取验证码后就可以开始注册流程了</p>
<p style="font-weight: 400">首先我们需要读取用户的输入并构建一个 EmailUser 对象,里面存放了邮箱用户的相关信息,包括邮箱地址,验证码和密码。这里用户在创建的时候可以选择是否设置密码,如果设置则后续在登录的时候就需要输入密码。</p>
<pre class="language-csharp"><code>string email = edtAccount.Text.ToString().Trim();
string password = edtPassword.Text.ToString().Trim();
string verifyCode = edtVerifyCode.Text.ToString().Trim();
// Build e-mail user.
EmailUser emailUser = new EmailUser.Builder()
.SetEmail(email)
.SetPassword(password)//optional,if you set a password, you can log in directly using the password next time.
.SetVerifyCode(verifyCode)
.Build();</code></pre>
<p style="font-weight: 400">EmailUser 创建成功后我们就可以调用 CreateUserAsync 方法创建用户了</p>
<pre class="language-csharp"><code>try
{
// Create e-mail user.
var emailUserResult = AGConnectAuth.Instance.CreateUserAsync(emailUser);
ISignInResult signInResult = await emailUserResult;
if (emailUserResult.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
{
// After a user is created, the user has logged in by default.
StartActivity(new Intent(this, typeof(MainActivity)));
}
}
catch (Exception ex)
{
Toast.MakeText(this, "Create User Fail:" + ex.Message, ToastLength.Long).Show();
}</code></pre>
<p style="font-weight: 400">用户创建完成后,SDK 会自动为用户进行登录,无需再调用登录接口了</p>
<p>3. 对于非首次使用的用户,我们就需要进行登录操作了,基于之前注册时的设置,登录分为两种,验证码登录和密码登录</p>
<pre class="language-csharp"><code>string email = edtAccount.Text.ToString().Trim();
string password = edtPassword.Text.ToString().Trim();
string verifyCode = edtVerifyCode.Text.ToString().Trim();
IAGConnectAuthCredential credential;
if (TextUtils.IsEmpty(verifyCode))
{
credential = EmailAuthProvider.CredentialWithPassword(email, password);
}
else
{
credential = EmailAuthProvider.CredentialWithVerifyCode(email, password, verifyCode);
}
SignIn(credential);
try
{
AGConnectAuth connectAuth = AGConnectAuth.Instance;
var signInResult = AGConnectAuth.Instance.SignInAsync(credential);
ISignInResult result = await signInResult;
if (signInResult.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
{
Log.Debug(TAG, signInResult.Result.ToString());
StartActivity(new Intent(this, typeof(MainActivity)));
Finish();
}
}
catch (Exception ex)
{
Log.Error(TAG, ex.Message);
Toast.MakeText(this, "SignIn failed: " + ex.Message, ToastLength.Long).Show();
}</code></pre>
<p style="font-weight: 400">我们可以看到,如果是密码登录,我们调用的是 CredentialwithPassword 来创建一个 credential,而验证码登录则是使用 CredentialWithVerifyCode 来创建 credential。而后我们调用 SignInAsync 方法传入这个 credential 进行登录的操作。</p>
<p style="font-weight: 400">以上我们就成功接入了 Xamarin 版本认证服务中的邮箱地址认证。如果觉得简单那就赶快行动起来试试吧。</p>
<h2 style="font-weight: 400">参考文档:</h2>
<p style="font-weight: 400">xamarin 使用入门:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-get-started-xamarin</p>
<p style="font-weight: 400">认证服务 - xamarin 使用指导:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-auth-xamarin-android-usage-0000001098592850#EN-US_TOPIC_0000001098592850__section17453164515224</p><br><br>
来源:https://www.cnblogs.com/developer-huawei/p/15897722.html
頁:
[1]