安缔 發表於 2022-2-16 09:57:00

如何在 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&nbsp;System;
using&nbsp;System.IO;
using&nbsp;Android.Util;
using&nbsp;Android.Content;
using&nbsp;Huawei.Agconnect.Config;

namespace&nbsp;XamarinHmsRemoteConfig
{
&nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;HmsLazyInputStream&nbsp;:&nbsp;LazyInputStream
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;HmsLazyInputStream(Context&nbsp;context)&nbsp;:&nbsp;base(context)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Get(context);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;override&nbsp;Stream&nbsp;Get(Context&nbsp;context)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;context.Assets.Open("agconnect-services.json");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch&nbsp;(Exception&nbsp;e)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.Error(e.ToString(),&nbsp;"Can't&nbsp;open&nbsp;agconnect&nbsp;file");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}</code></pre>
<p>在主 Activity 中,我们需要在 AttachBaseContext 中添加如下代码已读取 json 文件中的内容</p>
<pre class="language-csharp"><code>protected&nbsp;override&nbsp;void&nbsp;AttachBaseContext(Context&nbsp;context)
{
    base.AttachBaseContext(context);
    AGConnectServicesConfig&nbsp;config&nbsp;=&nbsp;AGConnectServicesConfig.FromContext(context);
    config.OverlayWith(new&nbsp;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&nbsp;settings&nbsp;=&nbsp;VerifyCodeSettings.NewBuilder()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Action(VerifyCodeSettings.ActionRegisterLogin)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SendInterval(30)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Locale(Locale.English)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build();</code></pre>
<p>读取用户输入的邮箱地址,调用 RequestVerifyCodeAsync 方法向认证服务服务器请求发送验证码,传入邮箱地址以及刚刚我们创建的 setting 对象</p>
<pre class="language-csharp"><code>string&nbsp;email&nbsp;=&nbsp;edtAccount.Text.ToString().Trim();
try
{
    var&nbsp;requestVerifyCode&nbsp;=&nbsp;AGConnectAuth.Instance.RequestVerifyCodeAsync(email,&nbsp;settings);
    VerifyCodeResult&nbsp;verifyCodeResult&nbsp;=&nbsp;await&nbsp;requestVerifyCode;
    if&nbsp;(requestVerifyCode.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
    {
&nbsp;&nbsp;&nbsp;&nbsp;    Toast.MakeText(this,&nbsp;"Send&nbsp;email&nbsp;verify&nbsp;code&nbsp;success!&nbsp;",&nbsp;ToastLength.Short).Show();
&nbsp;&nbsp;&nbsp;&nbsp;}
}
catch&nbsp;(Exception&nbsp;ex)
{
    Toast.MakeText(this,&nbsp;ex.Message,&nbsp;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&nbsp;email&nbsp;=&nbsp;edtAccount.Text.ToString().Trim();
string&nbsp;password&nbsp;=&nbsp;edtPassword.Text.ToString().Trim();

string&nbsp;verifyCode&nbsp;=&nbsp;edtVerifyCode.Text.ToString().Trim();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Build&nbsp;e-mail&nbsp;user.
EmailUser&nbsp;emailUser&nbsp;=&nbsp;new&nbsp;EmailUser.Builder()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetEmail(email)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetPassword(password)//optional,if&nbsp;you&nbsp;set&nbsp;a&nbsp;password,&nbsp;you&nbsp;can&nbsp;log&nbsp;in&nbsp;directly&nbsp;using&nbsp;the&nbsp;password&nbsp;next&nbsp;time.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetVerifyCode(verifyCode)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Build();</code></pre>
<p style="font-weight: 400">EmailUser 创建成功后我们就可以调用 CreateUserAsync 方法创建用户了</p>
<pre class="language-csharp"><code>try
{
    //&nbsp;Create&nbsp;e-mail&nbsp;user.
    var&nbsp;emailUserResult&nbsp;=&nbsp;AGConnectAuth.Instance.CreateUserAsync(emailUser);
&nbsp;&nbsp;&nbsp;&nbsp;ISignInResult&nbsp;signInResult&nbsp;=&nbsp;await&nbsp;emailUserResult;
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(emailUserResult.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;    //&nbsp;After&nbsp;a&nbsp;user&nbsp;is&nbsp;created,&nbsp;the&nbsp;user&nbsp;has&nbsp;logged&nbsp;in&nbsp;by&nbsp;default.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartActivity(new&nbsp;Intent(this,&nbsp;typeof(MainActivity)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
}
catch&nbsp;(Exception&nbsp;ex)
{
    Toast.MakeText(this,&nbsp;"Create&nbsp;User&nbsp;Fail:"&nbsp;+&nbsp;ex.Message,&nbsp;ToastLength.Long).Show();
}</code></pre>
<p style="font-weight: 400">用户创建完成后,SDK 会自动为用户进行登录,无需再调用登录接口了</p>
<p>3. 对于非首次使用的用户,我们就需要进行登录操作了,基于之前注册时的设置,登录分为两种,验证码登录和密码登录</p>
<pre class="language-csharp"><code>string&nbsp;email&nbsp;=&nbsp;edtAccount.Text.ToString().Trim();
string&nbsp;password&nbsp;=&nbsp;edtPassword.Text.ToString().Trim();
string&nbsp;verifyCode&nbsp;=&nbsp;edtVerifyCode.Text.ToString().Trim();
IAGConnectAuthCredential&nbsp;credential;
if&nbsp;(TextUtils.IsEmpty(verifyCode))
{
    credential&nbsp;=&nbsp;EmailAuthProvider.CredentialWithPassword(email,&nbsp;password);
}
else
{
    credential&nbsp;=&nbsp;EmailAuthProvider.CredentialWithVerifyCode(email,&nbsp;password,&nbsp;verifyCode);
}
SignIn(credential);
try
{
    AGConnectAuth&nbsp;connectAuth&nbsp;=&nbsp;AGConnectAuth.Instance;
&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;signInResult&nbsp;=&nbsp;AGConnectAuth.Instance.SignInAsync(credential);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;ISignInResult&nbsp;result&nbsp;=&nbsp;await&nbsp;signInResult;

&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(signInResult.Status.Equals(System.Threading.Tasks.TaskStatus.RanToCompletion))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;    Log.Debug(TAG,&nbsp;signInResult.Result.ToString());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StartActivity(new&nbsp;Intent(this,&nbsp;typeof(MainActivity)));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Finish();
    }
}
catch&nbsp;(Exception&nbsp;ex)
{
    Log.Error(TAG,&nbsp;ex.Message);

    Toast.MakeText(this,&nbsp;"SignIn&nbsp;failed:&nbsp;"&nbsp;+&nbsp;ex.Message,&nbsp;ToastLength.Long).Show();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}</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]
查看完整版本: 如何在 Xamarin 中快速集成 Android 版认证服务 - 邮箱地址篇